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
constant string too long
so can you get the size of string in there : get string size in bytes – Yasmeen