Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
536 views
in Technique[技术] by (71.8m points)

listview - java.lang.OutOfMemoryError: bitmap size exceeds VM budget

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...