0

Hi I am creating Bitmap object and using in canvas, like below code

Bitmap result = Bitmap.createBitmap(loadedImage.getWidth(), loadedImage.getHeight(), Config.ARGB_8888);
Canvas mCanvas = new Canvas(result);    
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
mCanvas.drawBitmap(blurBitmap, 0, 0, null);
mCanvas.drawBitmap(loadedImage, 0, 0, paint);
paint.setXfermode(null);

ivBlur.setImageBitmap(result);

// now i don't want Bitmap "result" so I am recycling it    
if (result != null && !result.isRecycled())
    result.recycle();
}

But from above code I am getting an error like

trying to use recycled bitmap android

How to fix "canvas: trying to use a recycled bitmap error"?

trying to use a recycled bitmap android.graphics.Bitmap

Canvas: trying to use a recycled bitmap android.graphics.Bitmap in android

If I don't write last three lines it is working perfect, It may be because Canvas is using Bitmap "result" object and I am recycling "result"

Because of this bitmap is taking huge space, I have to recycle this bitmap otherwise I will get

java.lang.OutofMemoryError: bitmap size exceeds VM budget.

So does anyone know how can I get that, "Canvas has used bitmap object and now I can recycle result object".

Community
  • 1
  • 1
Siddhpura Amit
  • 14,534
  • 16
  • 91
  • 150

1 Answers1

2

The issue isn't the the Canvas has used the bitmap. The issue is that you passed the Bitmap to an ImageView that needs it to draw itself Once you pass that Bitmap to an ImageView, you can't recycle it for as long as the ImageView is alive.

You can recycle the bitmap only after the ImageView with the Bitmap is removed from the view hierarchy and never used again. In that case, it's easier to just let the garbage collector handle it.

If you instead need a smaller Bitmap, you should create and draw into that smaller Bitmap before sending it to the ImageView.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • But @Doug Stevenson If I don't write ivBlur.setImageBitmap(result); I am still getting an Error, so I am sure it is problem with Canvas – Siddhpura Amit Mar 29 '16 at 03:43
  • 1
    It's mostly likely the size of the Bitmap. Canvas just draws into the memory allocated by the Bitmap. – Doug Stevenson Mar 29 '16 at 03:52
  • Yes that's why I want to recycle bitmap after use of Bitmap, so I can avoid memory related issue – Siddhpura Amit Mar 29 '16 at 04:00
  • Go back and read my answer again. You can't recycle the bitmap if you're going to use it in an ImageView. – Doug Stevenson Mar 29 '16 at 04:03
  • Sorry Doug but I have tried everyting... It is not creating problem when I first set bitmap on ImageView and recycle that... It is working perfectly.... It is creating problem on Canvas code only – Siddhpura Amit Mar 29 '16 at 04:06
  • I'm sorry you're having problems. Let me assure you I've done this dozens of times. You're encountering a race condition between the bitmap recycle and the ImageView render. It simply will not work in the long term. – Doug Stevenson Mar 29 '16 at 04:13