【发布时间】:2012-10-05 03:11:58
【问题描述】:
我遇到了非常严重的问题。 只有Android 4.1,Bitmap会自动回收! 我没有在我的代码中调用 recycle() ! 我的项目在任何分辨率的其他操作系统版本(〜4.0.3)中都可以正常工作。 其他项目也有同样的问题。
所有图像文件都在 drawable-nodpi 文件夹中。 我总是调整它们的大小以适应任何设备的分辨率。
public Bitmap GetBitmap(int resource){
BitmapFactory.Options options = new BitmapFactory.Options();
options.inDither = true;
options.inPurgeable = true;
Bitmap tmp = null;
try{
tmp = BitmapFactory.decodeResource(mResources, resource, options);
}catch(OutOfMemoryError e){
options.inSampleSize = 2;
tmp = BitmapFactory.decodeResource(mResources, resource, options);
}
return tmp;
}
public Bitmap GetScaledBitmap(int resource, int width, int height, boolean filter){
Bitmap tmp = GetBitmap(resource);
Bitmap img = Bitmap.createScaledBitmap(tmp, width, height, filter);
tmp.recycle();
tmp = null;
return img;
}
在我的测试中,
- 相同的位图实例,但根据调整大小会出现问题。
例如) 整数宽度 = 100;
位图 imgStar = MyResourceManager.getInstance().GetScaledBitmap(R.drawable.star, width, width , true); -> 返回回收的实例。
宽度 = 200;
imgStar = MyResourceManager.getInstance().GetScaledBitmap(R.drawable.star, width, width, true); -> 返回普通实例。
在不同的分辨率下,imgStar 工作正常,但在其他位图实例中出现问题。 同样,当我更改调整大小时,它工作正常。
在相同的分辨率下,如果我更改图像文件文件夹的名称,问题出现在其他位图实例中。 drawable-nodpi -> drawable -> drawable-ldpi, ..., drawable-xdpi.
相同的大小调整值,如果我输入其他资源 ID,它可以正常工作。 例如)
int 宽度 = 100;
位图 imgStar = MyResourceManager.getInstance().GetScaledBitmap(R.drawable.star, width, width , true); -> 返回回收的实例。
imgStar = MyResourceManager.getInstance().GetScaledBitmap(R.drawable.diamond, width, width, true); -> 返回普通实例。
请……我该怎么办?! T^T
【问题讨论】:
-
这个问题类似,可能有你的答案:stackoverflow.com/questions/7983321/…
标签: android bitmap drawable android-4.2-jelly-bean recycle