Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why BitmapFactory.decodeByteArray() returns null?

My app reads several BASE64 encoded images one by one from DB. But for some images(<0.1%), it fails to load. The method, BitmapFactory.decodeByteArray() return null which means its invalid format. But when I created a separate app with hard-coded culprit image, it worked. Now I have one big app where the same image didn't work and in my test app it works. Can anyone tell me why in God's name this is happening? Here is my code:

byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
Bitmap setBMPPath = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
if (setBMPPath != null) {
    qImage.setImageBitmap(setBMPPath);
}

Is there any other method that I can use to convert byte array to Bitmap?

like image 628
Jainendra Avatar asked May 22 '26 21:05

Jainendra


1 Answers

You can try adding Base64.NO_WRAP:

byte[] decodeResponse = Base64.decode(base64Image, Base64.DEFAULT | Base64.NO_WRAP);
Bitmap bitmap = BitmapFactory.decodeByteArray(decodeResponse, 0, decodeResponse.length);

Hope this helps someone.

like image 81
Zlatko Avatar answered May 24 '26 12:05

Zlatko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!