我的应用从网络加载大量图片并通过BitmapFactory.decodeByteArray
对其进行解码。但是,当解码应用程序时,许多图像会抛出错误java.lang.OutOfMemoryError
。所以在我在inPurgeable = true
中设置标记BitmapFactory.Options
之前,这个错误永远不会打扰我。
在Android 5字段中BitmapFactory.Options.inPurgeable
已弃用,并且在解码大量图片后再次抛出java.lang.OutOfMemoryError
。
我应该在Android 5中使用什么来避免此错误?
修改
我的代码:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPurgeable = true;
bitmap = BitmapFactory.decodeByteArray(buffer.toByteArray(), 0, buffer.length(), options);
一切。这在Android 4或更低版本上运行良好。
Android 5上的Stacktrace:
11-24 22:40:06.738: E/AndroidRuntime(19216): FATAL EXCEPTION: AsyncTask #3
11-24 22:40:06.738: E/AndroidRuntime(19216): java.lang.RuntimeException: An error occured while executing doInBackground()
11-24 22:40:06.738: E/AndroidRuntime(19216): at android.os.AsyncTask$3.done(AsyncTask.java:300)
11-24 22:40:06.738: E/AndroidRuntime(19216): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
11-24 22:40:06.738: E/AndroidRuntime(19216): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
11-24 22:40:06.738: E/AndroidRuntime(19216): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
11-24 22:40:06.738: E/AndroidRuntime(19216): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
11-24 22:40:06.738: E/AndroidRuntime(19216): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
11-24 22:40:06.738: E/AndroidRuntime(19216): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
11-24 22:40:06.738: E/AndroidRuntime(19216): at java.lang.Thread.run(Thread.java:818)
11-24 22:40:06.738: E/AndroidRuntime(19216): Caused by: java.lang.OutOfMemoryError: Failed to allocate a 1638412 byte allocation with 326188 free bytes and 318KB until OOM
11-24 22:40:06.738: E/AndroidRuntime(19216): at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
11-24 22:40:06.738: E/AndroidRuntime(19216): at android.graphics.BitmapFactory.nativeDecodeByteArray(Native Method)
11-24 22:40:06.738: E/AndroidRuntime(19216): at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:522)
...