java.lang.OutOfMemoryError:位图大小超出VM预算

发布于 2024-09-28 09:06:41 字数 1375 浏览 2 评论 0原文

所以我为我的 ListView 提供了一个惰性图像加载器。我还使用本教程来实现更好的内存管理,并且< code>SoftReference 位图图像存储在我的 ArrayList 中。

我的 ListView 工作从数据库加载 8 个图像,然后一旦用户一直滚动到底部,它就会加载另外 8 个图像,等等。当图像数量大约为 35 个或更少时,没有问题,但更多我的应用程序因 OutOfMemoryError 强制关闭。

我无法理解的是我的代码位于 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();
}

但是 try catch 块没有捕获 OutOfMemory 异常,并且根据我对 SoftReference 当应用程序内存不足时,应清除位图图像,从而阻止抛出 OutOfMemory 异常。

我在这里做错了什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

反差帅 2024-10-05 09:06:41

我想这篇文章可能会对你有所帮助

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

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;
}
不羁少年 2024-10-05 09:06:41

OutOfMemoryError 是一个错误而不是异常,您不应该捕获它。

请参阅http://mindprod.com/jgloss/exception.html

编辑:已知问题请参阅此问题

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

拥有 2024-10-05 09:06:41

Error 和 Exception 是 Throwable 的子类。
错误应该是如此严重,以至于你不应该抓住它们。

但你可以抓住任何东西。

try
{ 
}
catch (Throwable throwable)
{
}

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)
{
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文