Skip to content Skip to sidebar Skip to footer

How To Display Image (byte Array) From Json Into Imageview... Factory Returns Null

I'm struggling with this problem 2 days already... Was posting before but non of the answers were right. My problem is: i'm trying to convert byte[] into image. The byte[] comes fr

Solution 1:

For future reference

JSONArray jsonArray = newJSONArray(jsonString);
for (int i = 0; i < jsonArray.length(); i++) {
    HashMap<String, Object> map = newHashMap<String, Object>();
    JSONObject jsonObject = (JSONObject) jsonArray.get(i);
    int id = jsonObject.getInt("id");
    String name = jsonObject.getString("name");


    byte[] byteArray =  Base64.decode(jsonObject.getString("image"), Base64.DEFAULT) ;
    Bitmap bmp1 = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
    //Bitmap bitmap = Bitmap.createScaledBitmap(bmp1, 120, 120, false);


    map.put("id", id);
    map.put("name", name);

    map.put("image", bmp1);


    playersList.add(map);
    //Log.d("SHOW PLAYER LIST: " ,playersList.toString());
}

Solution 2:

Bitmapbitmap= BitmapFactory.decodeByteArray(byteArray , 0, byteArray .length);
 iv.setImageBitmap(bitmap );

This code will will simply decode image from the byte array .

Post a Comment for "How To Display Image (byte Array) From Json Into Imageview... Factory Returns Null"