开发者

Avoid bitmaps being recycled automatically in android

开发者 https://www.devze.com 2023-04-08 16:54 出处:网络
In my application, I am storing some bitmap images in the HashMap. with the hashmap as a member of a class ImageHolder.

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;
    }

}


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消