Advanced exception handling in Android

SDK Version: 

errorIn this tutorial I'll present you a simple exception handler class for Android. The class can invoke and display exceptions and it can be upgraded very easy for your further needs.

How to avoid OutOfMemory Error / OOM (The Ugly Truth Revealed)

SDK Version: 

ERROR/dalvikvm-heap(4204): 691456-byte external allocation too large for this process.
01-25 22:13:18.694: ERROR/(4204): VM won't let us allocate 691456 bytes 01-25 22:13:18.694

If you have ever got the message above, you are at the right place.

First of all the reason:
Heap size != External Memory size

Dalvik's external memory is limited to ~ 4MBs for each process (That is the Ugly Truth). If it overflows, you get the BitmapFactory.DecodeFile Error.
That's why even if you have like 2MB-s heap memory free, VM won't let you allocate ~700KB.
Since you can't modify external memory's size, you have to reduce your memory usage. This is the only solution I have, but it really works at least.

The solution:

Let say you have a nice big Gallery with lots of large images. An ImageAdapter class will provide all of the images that you are going to use. Therefore you'll need a List of ImageViews.

How to avoid OutOfMemory (OOM) Exception using Bitmaps (Solved)

SDK Version: 

Step 1.
First thing I want you to think about is that do You really need to use Bitmaps?
If the answer is No, go to step 2. :) Otherwise, think again.
Ok, Let me explain it. I needed Bitmaps to get the width of the images, to create an ImageAdapter for my Gallery. As far as I know, there's no other way to do that. So I used Bitmaps, and even if I recycled them, once in a while it stopped with the message: DDMS: OutOfMemory .. Phone: Force Close..
Not to speak about how laggy your app will be if you are using Bitmaps..
I've spent 2 weeks to find a solution for this problem, but I couldn't find any usable of them. Then I thought, I try not using Bitmaps anymore. So I have to find another way to get the width of images. Unfortunately, it's just a workaround, but it works so who cares, right?
I can determine widths with a PHP script, store it in my existing database. After I did that, everything was working properly.

Syndicate content