【发布时间】:2017-06-25 15:23:58
【问题描述】:
public static void writeBitmapWithCompress(final String localFileName, Bitmap b){
int bytes = b.getByteCount();
ByteBuffer buffer = ByteBuffer.allocate(bytes); //Create a new buffer
b.copyPixelsToBuffer(buffer); //Move the byte data to the buffer
byte[] array = buffer.array();
// byte[] encodedString = Base64.encode(array, Base64.DEFAULT);
byte[] decodedString = Base64.decode(array, Base64.DEFAULT);
// Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
Timber.d(">> social localFileName"+ localFileName);
try {
FileOutputStream fileOutputStream = new FileOutputStream(localFileName);
fileOutputStream.write(decodedString);
fileOutputStream.flush();
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
在这一行遇到异常 --> byte[] decodedString = Base64.decode(array, Base64.DEFAULT);
坏的base64。还有其他方法可以在不使用任何压缩的情况下完成这项任务吗?
堆栈跟踪--->
在 dalvik.system.VMRuntime.newNonMovableArray(本机方法) 在 android.graphics.BitmapFactory.nativeDecodeStream(Native Method) 02-08 在 android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:635) 我/dalvikvm:
在 android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:611)
在 android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:391)
在 android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:417)
在 nl.changer.socialschools.common.Utils.resizeImage(Utils.java:408)
在 nl.changer.socialschools.common.Utils.uploadPhotos(Utils.java:321) 在 nl.changer.socialschools.AsyncPostService.createPost(AsyncPostService.java:75) nl.changer.socialschools.AsyncPostService.onHandleIntent(AsyncPostService.java:50) 在 android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
在 android.os.Handler.dispatchMessage(Handler.java:110) 02-08 android.os.Looper.loop(Looper.java:193) 02-08 11:41:36.214 android.os.HandlerThread.run(HandlerThread.java:61)
【问题讨论】:
-
显示完整的异常堆栈跟踪和消息。它通常解释了问题。
-
你应该
encode将字节转换为 base64 字符串。不像你现在尝试做的decode。 -
但是为什么要使用base64呢?您可以将字节缓冲区直接写入文件。
-
如果我不解码,则无法在图库中看到图像。您还有其他选择吗?