我基本上是試圖壓縮,並通過由然而用戶在不同的手機使用的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'。 –