0

I have a base64 binary string which I got from an image in mongoDB. For example:

[255,216,255,224,0,16,74,70,73,70,0,1,1,1,0,96,0,96,0,0,255,219,0,67,0,8,6,6,7,6,5,8,7,7,7,9,9,8,10,12,20,13,12,11,11,12,25,18,19,15,20,29,26,31,30,29,26,28,28,32,36,46,39,32,34,44,35,28,28,40,55,41,44,48,49,52,52,52,31,39,57,61,56,50,60,46,51,52,50,255,219,0,67,1,9,9,9,12,11,12,24,13,13,24,50,33,28,33,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,.................,253,246,255,0,190,141,20,80,2,249,142,57,222,223,153,160,200,231,146,237,159,173,20,80,1,230,63,247,219,254,250,52,155,223,251,237,255,0,125,26,40,160,4,103,102,108,177,44,79,82,78,73,162,138,40,3,255,217]

In Android, how can I convert this string into a bitmap? This code does not work:

byte[] decodedString = {Base64 Binary Here}.getBytes();
Bitmap bmp = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);

This is my full code:

// JSON Parser
JsonParser jsonParser = new JsonParser();
JsonArray jsonArray = (JsonArray) jsonParser.parse(json_result);

String thirdParse = null;

for (int i = 0; i < jsonArray.size(); i++) {
    JsonObject object = (JsonObject) jsonArray.get(i);
    JsonObject firstParse = (JsonObject) object.get("img");
    JsonObject secondParse = (JsonObject) firstParse.get("data");
    thirdParse = secondParse.get("data").toString();
}

byte[] decodedString = thirdParse.getBytes();
Bitmap bmp = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);

imageView.setImageBitmap(bmp);

This is the JSON I get from mongoDB:

[{"img":{"data":{"type":"Buffer","data":[(base64 binary data)]}}}]
nestiank
  • 77
  • 2
  • 9
  • Check this answer https://stackoverflow.com/a/4837293/7586266 – Hossam Eldeen Onsy Jul 06 '19 at 06:45
  • May I ask does the mongodb get you an array of bytes or a string JSON ? – Hossam Eldeen Onsy Jul 06 '19 at 06:48
  • It returns a string of JSON. – nestiank Jul 06 '19 at 06:48
  • can you add the JSON that you receive in the question as well ? – Hossam Eldeen Onsy Jul 06 '19 at 06:54
  • I added the JSON you requested. – nestiank Jul 06 '19 at 06:57
  • okay i would like to ask you what is in the thirdParse = secondParse.get("data").toString(); as i have checked the decode locally and online and it worked so only remains is to see what is the parsing result , can you Log it and provide a sample of the first few lines here ? – Hossam Eldeen Onsy Jul 06 '19 at 07:43
  • For setText(thirdparse) in a textview I get something simillar to [255,216,255,224,0,16,74,70,73,70,0,1,1,1,0,96,0,96,0,0,255,219,0,67,0,8,6,6,7,6,5,8,7,7,7,9,9,8,10,12,20,13,.................,253,246,255,0,190,141,20,80,2,249,142,57,222,223,153,160,200,231,146,237,159,173,20,80,1,230,63,247,219,254,250,52,155,223,251,237,255,0,125,26,40,160,4,103,102,108,177,44,79,82,78,73,162,138,40,3,255,217]. Numbers are different. – nestiank Jul 06 '19 at 07:48
  • Possible duplicate of [How can I convert base64 to bitmap in Android](https://stackoverflow.com/questions/5106670/how-can-i-convert-base64-to-bitmap-in-android) – Qaiser Hussain Jul 06 '19 at 08:35

2 Answers2

0

Okay after a bit of diving in, the reason it didn't work is because here

byte[] decodedString = thirdParse.getBytes();

You are not taking the byte array from the string you are actually converting thirdParse into a bytearray so if you get for example thirdparse = [1,2,3] , when you use getBytes() it will convert this array to bytes and what you want is to take the values themselves directly not converting anything so here is what i did

first i created a method that you would give thirdParse to

public byte[] convertToByteArray(String stringContainingByteArray){
 String[] l =    stringContainingByteArray.replace("[","").replace("]","").split(",");
 byte [] byteArray = new byte[l.length];
 for (int i=0;i<l.length;i++){
     byteArray[i]=Byte.parseByte(l[i]);
 }
    return byteArray;
}

and you will just send it the thirdParse string and retrieve a byteArray which you will pass to the BitmapFactory as follows

    byte[] byteArray = convertToByteArray(stringContainingByteArray)
    Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
    imageView.setImageBitmap(bmp)

for simplicity the whole code without methods will be as follows

// JSON Parser
    JsonParser jsonParser = new JsonParser();
    JsonArray jsonArray = (JsonArray) jsonParser.parse(json_result);

    String thirdParse = null;

    for (int i = 0; i < jsonArray.size(); i++) {
        JsonObject object = (JsonObject) jsonArray.get(i);
        JsonObject firstParse = (JsonObject) object.get("img");
        JsonObject secondParse = (JsonObject) firstParse.get("data");
        thirdParse = secondParse.get("data").toString();
    }
    String[] l =    thirdParse.replace("[","").replace("]","").split(",");
    byte [] decodedString = new byte[l.length];
    for (int i=0;i<l.length;i++){
        decodedString[i]=Byte.parseByte(l[i]);
    }
    Bitmap bmp = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);

    imageView.setImageBitmap(bmp);
0

I just changed my image saving type from Buffer to String in mongoDB connection, and then changed encoding to base64 String.

nestiank
  • 77
  • 2
  • 9