joshapoorva joshapoorva - 4 months ago 23
Android Question

Android Listview and image decoding

try
{
if(json.getString(KEY_SUCESS)!=null)
{
String res=json.getString(KEY_SUCESS);
if(Integer.parseInt(res)==1)
{
JSONObject json_user = json.getJSONObject("user");
name=json_user.getString(KEY_NAME);

String image=json_user.getString(KEY_PHOTO);

map.put(KEY_NAME, name);

map.put(KEY_PHOTO, image);

List.add(map);


}
}


The photo is an String which is encoded in
base64
and i want to decode it and add to the map created but it take two string value and if i create a new map it would not add to that list view that i have created ,
what i have to do is to decode the string image to set it to a
ImageView
?

Answer

You don't show where your map is defined, but you get to choose what type of objects its key/value pair is.

It sounds like you want to take the string, decode into a bitmap and then store that into the map.

Define the map this way:

Map map = new HashMap<String, Bitmap>();

and then save it this way:

String image=json_user.getString(KEY_PHOTO);
byte[] decodedString = Base64.decode(image, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
map.put(KEY_PHOTO, bitmap);

And if you want to set the bitmap to an ImageView, you do the following.

imageView.setImageBitmap(bitmap);
Comments