Sunday, August 25, 2013

Handle error of 'OutOfMemoryError' and 'Bitmap too large to be uploaded into a texture' for images/bitmaps

Before start this post, I have to mention that: IT'S STRONGLY NOT RECOMMENDED TO USE THE APPROACH HERE.

In the previous exercise for image processing, I haven't handle resizing of the images/bitmaps. It will cause the app close, or no display on ImageView, if you try to load with large images. Mainly there are two problem here:

- java.lang.OutOfMemoryError

It's due to Heap Size not enough to handle the bitmaps, you can try to require large heap by defining android:largeHeap="true" inside <application> in AndroidManifest.xml file. In my trial experience, after defined it, the app can handle the original 3264x2448 photos taken by HTC One X, but cannot handle original 4256x2832 photos from Nikon D700 DSLR.

- Bitmap too large to be uploaded into a texture (..., max=2048x2048)

It's due to OpenGLRenderer hardware Acceleration cannot handle bitmap larger than 2048x2048. You can try to disable hardware Acceleration by defining android:hardwareAccelerated="false" inside <application> in AndroidManifest.xml file.

Finally, your AndroidManifest.xml will look like this:

    <application
        ...
        android:hardwareAccelerated="false"
        android:largeHeap="true" >

Exercise of "Merge images with PorterDuffXfermode" with these modification to handle original photos by HTC One X.

Anyway, both method cannot solve the problem perfectly, so it's not recommended.



Sometimes, even you have enough memory but not in single block. For example, you have three 1K memory block in seperated area, but you request memory of 1.5K, you will get 'OutOfMemoryError' also!

Good official lessons teach Caching Bitmaps and Managing Bitmap Memory, it is recommended to read if you need to handle a number of images in your app.


more: Something about processing images in Android

4 comments:

Nguyễn Long said...

you save my life :)) thank you

Manoj Tarkar said...

I have same issue.
I am not able to add texture image of 2048*2048.
could you please guide me.
thanks

Monica Memane said...

15530Thanks a lot..It helped :)

Firas Rassas said...

THANK YOU VERY MUCH !!! YA SAVE MY LIFEEEE !!!!