7

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?

Community
  • 1
  • 1
mlevit
  • 2,676
  • 10
  • 42
  • 50

3 Answers3

9

I suppose may be this post will help you

//decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f){
    try {
        //Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f),null,o);

        //The new size we want to scale to
        final int REQUIRED_SIZE=70;

        //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<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
                break;
            width_tmp/=2;
            height_tmp/=2;
            scale*=2;
        }

        //Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize=scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {}
    return null;
}
Aaron C
  • 3,328
  • 1
  • 24
  • 22
Narasimha
  • 3,802
  • 11
  • 55
  • 83
4

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

  • 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 Oct 18 '10 at 05:11
  • It makes perfectly good sense to catch OutOfMemoryError, if one has a way to resolve the problem, or if one wants to tell the user by for instance starting a new activity in a separate proces. – arberg Dec 07 '10 at 12:36
0

Error and Exception are subclassed from Throwable. Error are supposed to be so drastic, that you should not catch them.

But you can catch anything.

try
{ 
}
catch (Throwable throwable)
{
}
Swoogan
  • 5,298
  • 5
  • 35
  • 47