Vik*_*aka 6 base64 android bitmapfactory
我有一台服务器,里面有几张1.5 kb到9 Mb的照片.来自PC,平板电脑和手机的照片.服务器将它们编码为Base64字符串,然后将它们发送到Android客户端.在BitmapFactory.decodeByteArray中解码时,一个300 kb的照片返回null ...但是它是有效的图像并且在在线解码器中被很好地解码.
byte[] decodedString = Base64.decode(image64, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, ecodedString.length);
Run Code Online (Sandbox Code Playgroud)
2天我找不到答案(
有任何想法吗?谢谢!
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;
}
Run Code Online (Sandbox Code Playgroud)
和logcat
好形象:
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
Run Code Online (Sandbox Code Playgroud)
坏图像:
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
Run Code Online (Sandbox Code Playgroud)
小智 0
这可能不是您正在寻找的答案,但您是否考虑过使用框架?我一直在使用 Picasso,它就像这样简单: Picasso.with(context).load(" https://i.stack.imgur.com/E5w9Z.jpg ").into(imageView);
http://square.github.io/picasso/