【发布时间】:2015-12-22 21:07:31
【问题描述】:
我基本上是在尝试压缩和传递用户选择的图像的 Base64 表示,但是应用程序在不同的手机上崩溃并出现 OutOfMemoryError 问题。这是我的压缩和转换代码:
Bitmap bm = BitmapFactory.decodeFile(filePath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] byteArrayImage = baos.toByteArray();
String base64String = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);
这个过程也非常缓慢,有时会导致应用崩溃。
这是我得到的一个例外:
java.lang.OutOfMemoryError: Failed to allocate a 5035548 byte allocation with 5011320 free bytes and 4MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:625)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:460)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:973)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2477)
我应该做哪些改变?
【问题讨论】:
标签: java android performance android-activity bitmap