我基本上是试图压缩,并通过由然而用户在不同的手机使用的OutOfMemoryError问题的应用程序崩溃选定的图像的Base64表示。这里是我的压缩和转换代码:Android的位图压缩不足,导致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)
我应该做什么样的变化?
感谢您的详细解释。使用此解决方案压缩后输出图像的质量如何?它受到很大影响吗? – Dinuka
在平均图像上看起来不错,但图像越高,需要的时间就越长。我们可以加快这一点吗? – Dinuka
是的,你可以为此你必须缩放图像。看看'Edit 1'。 –