Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Out Of Memory with images in Android 5

My app loading lot of images from network and decode it via BitmapFactory.decodeByteArray. But when the application is decoded many images throws error java.lang.OutOfMemoryError. So before i set flag inPurgeable = true in BitmapFactory.Options and this error will never not bother me.

In Android 5 field BitmapFactory.Options.inPurgeable is deprecated and java.lang.OutOfMemoryError is thrown again after decoding a large amount of images.

What I should use in Android 5 to avoid this error?

EDIT:

My code:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inPurgeable = true;
bitmap = BitmapFactory.decodeByteArray(buffer.toByteArray(), 0, buffer.length(), options);

It all. This works fine on Android 4 or less.

Stacktrace on Android 5:

    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)
...
like image 414
Miraslau Rachkouski Avatar asked Nov 10 '22 22:11

Miraslau Rachkouski


1 Answers

Resolved the problem using this guide: http://developer.android.com/training/displaying-bitmaps/index.html

like image 150
Miraslau Rachkouski Avatar answered Nov 14 '22 22:11

Miraslau Rachkouski