1

I need to convert a json array of byte to the bitmap image using android.i got json response like this

{"type":"Buffer","data":[137,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82,0,0,1,140,0,0,1,34,4,3,0,0,0,24,1,248,231,0,0,0,21,80,76,84,69,255,255,255,0,0,0,220,217,207,0,100,0,176,196,222,0,153,204,204,255,255,227,179,150,92,0,0,1,29,73,68,65,84,120,156,237,207,1,13,128,64,12,4,193,211,130,5,44,96,1,11,248,151,192,91,104,66,66,191,153,85,176,147,99,68,249,123,224,155,48,58,181,24,217,62,140,78,205,98,156,165,146,171,84,114,151,74,158,82,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,157,25,187,135,209,169,73,140,9,97,116,106,8,227,5,178,0,69,109,104,207,36,38,0,0,0,0,73,69,78,68,174,66,96,130]} 

this image byte array is read from postgres DB(type:Bytea).

How to convert the byte array to actual image.

this is my code for converting data

img = profPic.getJSONArray("data");
bImage = Base64.decode(img.toString().getBytes(), Base64.DEFAULT);
bmp = BitmapFactory.decodeByteArray(bImage, 0, bImage.length);
5
  • What is in bImage? What is in img? Why would you think you could turn a JSONArray into a bitmap? Commented Mar 24, 2016 at 9:10
  • bImage is a byte array.i am getting some tumbnails from server in json array format.i am trying to convert it to byte array and then to bitmap. Commented Mar 24, 2016 at 9:40
  • What does the CONTENTS of bImage look like? What does the CONTENTS of img look like? Commented Mar 24, 2016 at 10:28
  • it contains {-41,126,4,-45,-65.......} Commented Mar 24, 2016 at 11:06
  • my jsonarray contain [137,80,78,71,13,....] Commented Mar 24, 2016 at 11:09

2 Answers 2

3

Try this

JSONObject obj=new JSONObject("{\"type\":\"Buffer\",\"data\":[137,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82,0,0,1,140,0,0,1,34,4,3,0,0,0,24,1,248,231,0,0,0,21,80,76,84,69,255,255,255,0,0,0,220,217,207,0,100,0,176,196,222,0,153,204,204,255,255,227,179,150,92,0,0,1,29,73,68,65,84,120,156,237,207,1,13,128,64,12,4,193,211,130,5,44,96,1,11,248,151,192,91,104,66,66,191,153,85,176,147,99,68,249,123,224,155,48,58,181,24,217,62,140,78,205,98,156,165,146,171,84,114,151,74,158,82,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,157,25,187,135,209,169,73,140,9,97,116,106,8,227,5,178,0,69,109,104,207,36,38,0,0,0,0,73,69,78,68,174,66,96,130]} ");
Bitmap bitmap=null;
byte[] tmp=new byte[obj.getJSONArray("data").length()];
for(int i=0;i<obj.getJSONArray("data").length();i++){
     tmp[i]=(byte)(((int)obj.getJSONArray("data").get(i)) & 0xFF);
}
bitmap= BitmapFactory.decodeByteArray(tmp, 0, tmp.length);
Sign up to request clarification or add additional context in comments.

Comments

0

Try this

byte[] bitmapdata = blob.toByteArray();
Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata, 0, bitmapdata.length);

1 Comment

And where does blob come from?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.