mle*_*vit 7 java listview bitmap out-of-memory
所以我有一个懒惰的图像加载器ListView.我还使用本教程来更好地管理内存并将SoftReferenceBitmap图像存储在我的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();
}
Run Code Online (Sandbox Code Playgroud)
但是try catch块没有捕获OutOfMemory异常,根据我的理解,SoftReference当应用程序内存不足时,应该清除Bitmap映像,从而停止OutOfMemory抛出异常.
我在这做错了什么?
我想这篇文章可能会对你有所帮助
//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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10152 次 |
| 最近记录: |