java.lang.OutOfMemoryError:位图大小超出VM预算
所以我为我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想这篇文章可能会对你有所帮助
I suppose may be this post will help you
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
Error 和 Exception 是 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.