Android BitmapFactory returns null for Base64 byte array

I have a server with several photos from 1.5 kb to 9 mb. Photos from PCs, tablets and phones. The secret will encode them to Base64 strings, and then send them to the Android client. One 300 kb photo returns null when decoding in BitmapFactory.decodeByteArray ... But this is a valid image and good decoding in an online decoder.

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

Within 2 days I can not find the answer (

Any ideas? Thanks!

PS

  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 picture:

 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 
+5
source share
2 answers

This may not be the answer you are looking for, but do you consider using it? I used Picasso and is just as simple: Picasso.with (context) .load (" http://i.imgur.com/DvpvklR.png ") .into (imageView);

http://square.imtqy.com/picasso/

0
source

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

For this you can use the substring method, it will work.

0
source

All Articles