I got confused on the conversion from PHP/JSON, where the imagename(from Mysql) is printed as a long series of characters echo json_encode($response);
but in echo '<img src...
an image has been displayed. TAG_IMAGE_NAME will contain the Byte Array in String from JSONArray['imageName'] and Include the Image in the Hashmap.
What I wanted is to convert IT(returned from JSONObject 'imagename' using java) to images then store it in the sd card and Populate the image in the listView. Sorry for being confused. Thanks for your consideration.
Table: imagename
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...."}]}
Android/Java/JSONParser:
protected String doInBackground(String... 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);
}
});
}