0

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);
Josemafuen
  • 682
  • 2
  • 16
  • 41
  • 1
    When you call `mapBitmap = Bitmap.createBitmap(map, 0, 0, map.getWidth(), map.getHeight());` You are using the `map` object which is a recycled Bitmap and thus the error – MatPag Jun 12 '17 at 18:46
  • please post full code for review and detailed recommendations. – apelsoczi Jun 12 '17 at 18:54
  • there is a simple answer: **you can't** reuse recycled bitmap, as there is nothing to reuse, since it is **recycled**, dead, errazed, removed, garbage-collected. It does not exist any more. – Vladyslav Matviienko Jun 12 '17 at 19:03
  • @MatPag you're right, it seems like when I call recycle on mapBitmap, map is also recycled so I have to reinstantiate both of them. Put your comment as an answer and I'll mark it as correct. – Josemafuen Jun 12 '17 at 21:23
  • @JOSEMAFUEN Done ;) – MatPag Jun 12 '17 at 21:46

1 Answers1

0

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.

MatPag
  • 41,742
  • 14
  • 105
  • 114