避免位图在 android 中被自动回收
在我的应用程序中,我在 HashMap 中存储一些位图图像。 将哈希图作为 ImageHolder 类的成员。 我需要程序中 HashMap 中的图像来进一步处理图像。 当我运行我的应用程序时,位图图像会自动回收,我得到
java.lang.IllegalStateException: 无法在回收的位图上调用 getPixels()。
请帮助我避免位图图像被自动回收。
这是类 ImageHolder 的代码:
public class ImageHolder {
private HashMap<String, Bitmap> mImageMap = null;
public ImageHolder() {
mImageMap = new HashMap<String, Bitmap>();
}
/**
* @return the mImageMap
*/
public Map<String, Bitmap> getmImageMap() {
return mImageMap;
}
/**
* @param mImageMap the mImageMap to set
*/
public void setmImageMap(HashMap<String, Bitmap> mImageMap) {
this.mImageMap = mImageMap;
}
}
In my application, I am storing some bitmap images in the HashMap.
with the hashmap as a member of a class ImageHolder.
I need the images in the HashMap in my program for further processing on the images.
When I run my application, the bitmap image is automatically recycled and I get
java.lang.IllegalStateException: Can't call getPixels() on a recycled bitmap.
Please help me to avoid the bitmap image being recycled automatically.
Here is code for class ImageHolder :
public class ImageHolder {
private HashMap<String, Bitmap> mImageMap = null;
public ImageHolder() {
mImageMap = new HashMap<String, Bitmap>();
}
/**
* @return the mImageMap
*/
public Map<String, Bitmap> getmImageMap() {
return mImageMap;
}
/**
* @param mImageMap the mImageMap to set
*/
public void setmImageMap(HashMap<String, Bitmap> mImageMap) {
this.mImageMap = mImageMap;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
加载位图时读取像素并将这些信息存储在某处。
我不知道您为什么需要这些像素,但我建议您不要在较长时间内存储像素信息,因为这确实占用内存。
Read the pixels when you load the bitmap and store these information somewhere.
I dont know why you need these pixels, but I can recommend you NOT to store the pixel information over a longer periode of time cause this really eats the memory.
如果没有完整的源代码,很难说清楚。您很可能在多个地方使用相同的位图,例如 ImageView 和 Holder 类。如果其中一个导致回收,它将影响另一个,因为两者都引用同一个对象。如果您这样做,您可能需要使用 bitmap.copy 方法。
Its kind of hard to tell without your whole source code. Most probably you are using the same bitmap in multiple places, like ImageView and the Holder class. If the either causes a recycle, it will affect the other as both are referencing the same object. If you are doing this, you might want to use the bitmap.copy method.