Search code examples
androidcanvasbitmaprecycle

Reinitialize recycled Bitmap Android


What I'm trying to do is to reuse a Bitmap after recycling it. For doing that, I know that I have to initialize de Bitmap again, I'm doing it this way after calling recycle():

mapBitmap = Bitmap.createBitmap(map, 0, 0, map.getWidth(), map.getHeight());

but when I try to use it, i get

06-12 20:41:01.628 615-1470/com.example.project W/System.err: java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@5f1fba3

By the way, I have another Bitmap which I have to recycle and use later on, but this one works perfectly, the only difference between them is that I'm initiallizating this one using decodeFile() as follows:

bm = BitmapFactory.decodeFile(url);

Solution

  • Your problem is that you are using a recycled Bitmap to initialize another one. In this line:

    mapBitmap = Bitmap.createBitmap(map, 0, 0, map.getWidth(), map.getHeight());
    

    you are using the map object which is a recyled Bitmap, and you can't create a new Bitmap using a recycled one, be sure to initialize it properly before calling Bitmap.createBitmap(map, ...) or don't call map.recycle() somewhere in your code before you have finished using it.