views:

111

answers:

1

Hey guys,

So I've got a lazy image loader for my ListView. I also use this tutorial for better memory management and have SoftReference Bitmap images stored in my ArrayList.

My ListView works loads 8 images from a DB then once the user scrolls all the way to the bottom it loads another 8 etc etc. There was no issue when there were around 35 images or less, but any more and my app Force Closes with OutOfMemoryError.

The thing that I can't understand is I have my code inside a try catch:

try
{
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeByteArray(image, 0, image.length, o);

    //Find the correct scale value. It should be the power of 2.
    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    int scale = 1;

    while(true)
    {
        if(width_tmp/2 < imageWidth || height_tmp/2 < imageHeight)
        {
            break;
        }

        width_tmp/=2;
        height_tmp/=2;
        scale++;
    }

    //Decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    bitmapImage = BitmapFactory.decodeByteArray(image, 0, image.length, o2);        
}
catch (Exception e)
{
    e.printStackTrace();
}

But the try catch block isn't catching the OutOfMemory exception and from what I understand the SoftReference Bitmap images should be cleared when the application is running out of memory stopping the OutOfMemory exception being thrown.

What am I doing wrong here?

A: 

OutOfMemoryError is an error not an exception, you should not catch it.

see http://mindprod.com/jgloss/exception.html

EDIT: known problem see this issue

RC
Ah my bad... didn't know that at all. Is there anything I can do to prevent it from happening? I'm completely stuck.
mlevit