我对从 PHP/JSON 的转换感到困惑,其中图像名(来自 Mysql)被打印为一长串字符 echo json_encode($response);
,但在echo '<img src...
图像中已显示。TAG_IMAGE_NAME 将包含来自 JSONArray['imageName'] 的字符串中的字节数组,并将图像包含在Hashmap中。
我想要的是将IT(使用java从JSONObject'imagename'返回)转换为图像,然后将其存储在sd卡中并在listView中填充图像。很抱歉让您感到困惑。感谢您的考虑。
表:图像名称
2 | (Binary/Image) | 32byte
3 | (Binary/Image) | 9byte
php/json:
while ($row = mysql_fetch_array($result)) {
// temp user array
$product = array();
$product["pid"] = $row["groupId"];
$product["name"] = $row["description"];
$img = $row["imageName"];
$b64img = base64_encode ($img);
$b64img = mysql_real_escape_string($b64img);
$product["imageName"] = $b64img;
//echo '<img src="data:image/jpg;base64,' . base64_encode($img) . '" />';
}
……
// echoing JSON response
echo json_encode($response);
{"products":[{"pid":"BEER","name":"sample","imageName":"\/9j\/4AAQSkZJRgABAQEAAAAAAAD...."}]}
安卓/Java/JSONParser:
受保护的字符串doInBackground(字符串... args){
.
.
.
String TAG_IMAGE_NAME = "imageName"; //WILL contain the Byte Array in String
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_PID);
String name = c.getString(TAG_NAME);
//Confused with this part
byte[] decodedString = Base64.decode(c.getString(TAG_IMAGE_NAME), Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_PID, id);
map.put(TAG_NAME, name);
// adding HashList to ArrayList
productsList.add(map);
}
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, productsList,
R.layout.list_item, new String[] { TAG_PID,
TAG_NAME},
new int[] { R.id.pid, R.id.name , R.id.list_image});
// updating listview
setListAdapter(adapter);
}
});
}