java.lang.OutOfMemoryError: 位图大小超出 VM 预算
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3956702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.OutOfMemoryError: bitmap size exceeds VM budget
提问by mlevit
So I've got a lazy image loader for my ListView
. I also use this tutorialfor better memory management and have SoftReference
Bitmap images stored in my ArrayList
.
所以我有一个懒惰的图像加载器用于我的ListView
. 我还使用本教程进行更好的内存管理,并将SoftReference
位图图像存储在我的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
.
我的ListView
作品从数据库加载 8 个图像,然后一旦用户一直滚动到底部,它就会加载另外 8 个等等。当大约有 35 个或更少的图像时没有问题,但是更多并且我的应用程序 Force Closes with OutOfMemoryError
.
The thing that I can't understand is I have my code inside a try catch:
我无法理解的是我的代码在 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.
但是 try catch 块没有捕获OutOfMemory
异常,据我所知,SoftReference
当应用程序内存不足时,应清除位图图像以停止OutOfMemory
抛出异常。
What am I doing wrong here?
我在这里做错了什么?
采纳答案by mlevit
OutOfMemoryError
is an error not an exception, you should not catch it.
OutOfMemoryError
是错误而不是异常,您不应该捕获它。
see http://mindprod.com/jgloss/exception.html
见http://mindprod.com/jgloss/exception.html
EDIT: known problem see this issue
编辑:已知问题请参阅此问题
回答by Narasimha
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;
}
回答by Jim Phillips
Error and Exception are subclassed from Throwable. Error are supposed to be so drastic, that you should not catch them.
Error 和 Exception 是 Throwable 的子类。错误应该是非常严重的,你不应该抓住它们。
But you can catch anything.
但是你可以抓住任何东西。
try
{
}
catch (Throwable throwable)
{
}