Monday, November 11, 2013

Canvas: trying to use a recycled bitmap

Error

Canvas: trying to use a recycled bitmap


This error happened while recycling Bitmaps in LRUCache entryRemoved while a list view is still trying to show one of the recycled images

OS: Android 2.3.3



Solution


My solution was to override ImageView and do a check in order not to draw recycled images




public class MyNewsListImageView extends ImageView {
   
    public MyNewsListImageView(Context ctx) {
        super(ctx);
    }
   
    public MyNewsListImageView(Context ctx, AttributeSet set) {
        super(ctx,set);
    }
   
    public MyNewsListImageView(Context context, AttributeSet attrs, int defStyle)
    {
       
        super(context,attrs,defStyle);
    }

   
   
    @Override
    public void draw(Canvas canvas) {
       
        Drawable drawable = getDrawable();
        if (drawable!=null && drawable instanceof BitmapDrawable)
        {
            Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
           
            if ( bitmap.isRecycled())
            {
                MyLogger.d(Constants.DEBUG_TAG2,"trying to draw RECYCLED Bitmap >>> ignoring");
                return;
            }
           
            MyLogger.d(Constants.DEBUG_TAG2,"trying to draw FRESH Bitmap >>> passed");
        }
        super.draw(canvas);
    }

}



<mypackage.MyNewsListImageView android:id="@+id/newsImage" android:layout_width="fill_parent" android:layout_height="wrap_content" android:adjustViewBounds="false" android:background="@null" android:maxHeight="300sp" android:minHeight="200dp" android:paddingBottom="10sp" android:scaleType="fitXY" android:visibility="gone" />

No comments:

Post a Comment