6
votes

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
2
what differenciate the images, except their size? - goto
My friend can you give us your image for try it ? - Yasin Kaçmaz
All photos jpg. Problem from Android Tablet - Viktor Burmaka
The string is too large it's in txt file: dropbox.com/s/3n4doyimeo8v9lz/base64image.txt?dl=0 - Viktor Burmaka
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 bytes - Yasin Kaçmaz

2 Answers

0
votes

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("http://i.imgur.com/DvpvklR.png").into(imageView);

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

0
votes

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

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