Android BitmapFactory returning null on Base64 decoded byte array
Asked Answered
C

2

6

I have a server with several photos from 1.5 kb to 9 Mb. The photos from PC, tablets and phones. The sever encode them to Base64 strings and then send them to an Android client. One 300 kb photo return null when decoding in BitmapFactory.decodeByteArray... But it's valid image and good decoded in online decoder.

byte[] decodedString = Base64.decode(image64, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, ecodedString.length);

For 2 days I can not find the answer (

Any ideas? Thanks!

P.S.

 private boolean decodeImage64(String uid, String image64, String name) {
    Bitmap decodedByte;
    boolean result = false;
    if (image64 != null && !image64.isEmpty()) {

        try {
            Log.e(TAG, "decodeImage64: image64.getBytes().length = " + image64.getBytes().length);
            byte[] decodedString = Base64.decode(image64, Base64.DEFAULT);
            Log.e(TAG, "decodeImage64: decodedString = " + decodedString + "  , decodedString.length = " + decodedString.length);
            decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
            Log.e(TAG, "decodeImage64: decodedByte = " + decodedByte);

            if (decodedByte != null) {
                FileOutputStream out = null;
                try {
                    out = new FileOutputStream(getImageFolderName() + "/" + uid + ".png");
                    decodedByte.compress(Bitmap.CompressFormat.PNG, 100, out);
                    decodedByte.recycle();
                    out.close();

                } catch (Exception e) {
                    Log.e(TAG, Log.getStackTraceString(e));
                } finally {
                    try {
                        if (out != null) {
                            out.close();
                        }
                        if (decodedByte != null){
                            decodedByte.recycle();
                        }
                    } catch (IOException e) {
                        Log.e(TAG, Log.getStackTraceString(e));
                    }
                }
                result = true;
            }else {
                Log.e(TAG, "  !!!!!!!!!!!!!!!!!!!!!!! decodeImage64: decodedByte = null "  + name);
            }
        }catch (Exception e){
            Log.e(TAG, Log.getStackTraceString(e));
        }
    } else {
        Log.e(TAG, "decodeImage64: image = null " + name);
    }
    return result;
}

And logcat

good image:

06-29 02:33:57.465 18197-18584/cps.agrovisio E/myLogs:  ------------------------- doInBackground: Good photo
06-29 02:34:13.993 18197-18584/cps.agrovisio E/myLogs: decodeImage64: image64.getBytes().length = 2264744
06-29 02:34:14.085 18197-18584/cps.agrovisio E/myLogs: decodeImage64: decodedString = [B@bb8956d  , decodedString.length = 1676499
06-29 02:34:14.635 18197-18584/cps.agrovisio E/myLogs: decodeImage64: decodedByte = android.graphics.Bitmap@a6d05a2

bad image:

06-29 02:33:56.041 18197-18584/сps.agrovisio E/myLogs:  ------------------------- doInBackground: Bad photo 
06-29 02:33:57.177 18197-18584/cps.agrovisio E/myLogs: decodeImage64: image64.getBytes().length = 372570
06-29 02:33:57.194 18197-18584/cps.agrovisio E/myLogs: decodeImage64: decodedString = [B@abcf243  , decodedString.length = 275799
06-29 02:33:57.245 18197-18584/cps.agrovisio E/myLogs: decodeImage64: decodedByte = null
Chemism answered 28/6, 2016 at 22:7 Comment(9)
what differenciate the images, except their size?Perihelion
My friend can you give us your image for try it ?Yasmeen
All photos jpg. Problem from Android TabletChemism
The string is too large it's in txt file: dropbox.com/s/3n4doyimeo8v9lz/base64image.txt?dl=0Chemism
I think the problem is your String bigger than 64k , when i want to add to my project it give me constant string too long so can you get the size of string in there : get string size in bytesYasmeen
@ViktorBurmaka Actually i paste your String to this site : Base64 to image and the result : Total Characters : 382324 and size 373.36KBYasmeen
@Yasin Kaçmaz Thanks! I'll try. But I successfully decoded 9 MB, and there is only 300 kbChemism
@ViktorBurmaka i think size doesnt matter, maybe you can post your method and logcat and we will think about it ?Yasmeen
@Yasin Kaçmaz Thank you for your attention! I edited the questionChemism
U
0

This may not be the answer you're looking for but have you considered using a framework? I've been using Picasso and it's as easy as: Picasso.with(context).load("https://i.sstatic.net/E5w9Z.jpg").into(imageView);

http://square.github.io/picasso/

Udo answered 28/6, 2016 at 22:43 Comment(1)
Thanks! But I can't use Picasso. I got a few parameters in json, image in base64 one of themChemism
U
0

Slice the part data:image/jpg;base64, from image64. Only have the encoded string.

You can use substring method for this, it will work.

Unpolite answered 14/10, 2016 at 14:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.