【发布时间】:2011-04-26 18:46:28
【问题描述】:
所以我的ListView 有一个惰性图像加载器。我还使用this tutorial 进行更好的内存管理,并将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 异常,据我了解,当应用程序内存不足停止抛出OutOfMemory 异常时,应该清除SoftReference 位图图像。
我在这里做错了什么?
【问题讨论】:
标签: java listview bitmap out-of-memory