Recycling bitmap with reflection library.

nad

Active Member
Licensed User
Longtime User
Hello,

I am having the following problem. I am trying to add images to a ListView with a small icon representing each image. It works perfectly for a small amount of images but not when i have 200. I am trying to recycle Bitmap1 each time after it gets asigned to the ListView and it finishes correctly but after that (after finishing the assignation to the ListView without errors i get an error at the end). I am using Andrew Graham reflection library to recycle the bitmap1 because if i have many images i get an out of memory.

For i = 0 To Bitmaps.Size-1
Log("Bitmaps.Get("&i&"):"&Bitmaps.Get(i))
Bitmap1.InitializeSample(File.DirAssets, Bitmaps.Get(i),50dip,50dip)
ListView1.AddTwoLinesandBitmap(Nombres.get(i) ,Nombres2.Get(i) , Bitmap1)
Obj1.Target=Bitmap1
Obj1.RunMethod("recycle")
Next
Log("Finished loading image icons:"&i)




Bitmaps.Get(224):225youreallygome.gif
Bitmaps.Get(225):226yousangtome.gif
Finished loading image icons:226


** Activity (main) Resume **
Shutting down VM


threadid=1: thread exiting with uncaught exception (group=0x4001d800)


FATAL EXCEPTION: main
java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@44f26618
at android.graphics.Canvas.throwIfRecycled(Canvas.java:955)
at android.graphics.Canvas.drawBitmap(Canvas.java:1044)
at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:323)
at android.view.View.draw(View.java:6725)
at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
at android.widget.AbsListView.dispatchDraw(AbsListView.java:1365)
at android.widget.ListView.dispatchDraw(ListView.java:3046)
at android.view.View.draw(View.java:6846)
at android.widget.AbsListView.draw(AbsListView.java:2257)
at android.view.ViewGroup.drawChild(ViewGroup.java:1640)

Please help. What am i doing wrong. Is it not the correct method to recycle the bitmap? why is it failing?

Thanks a lot and best regards,
 

agraham

Expert
Licensed User
Longtime User
You can't recycle a bitmap while it is still in use, that is the whole point of recycling it. While the bitmaps are still added to the ListView then they are in use. It looks like you are trying to use too many different bitmaps of too large a size and are running out of memory. You have hit a device limitation.

Also look at the actual sizes that InitializeSample is returning. Although you ask for 50dip x 50dip the actual sizes can be much larger, in the worse case nearly four times the number of pixels you are expecting because InitializeSample only works with integer scalings of the original bitmap. For example a bitmap that is actually 99dip by 99dip may not get resized at all whereas one that is 100dip by 100 dip will be.

You might be better off opening the bitmaps normally and drawing them on another of the size you need. Also if you can add the same bitmap to more than one ListView entry that will save memory.
 
Upvote 0

nad

Active Member
Licensed User
Longtime User
Thanks a lot Andrew. I will try to change my code as you say.

Thanks and best regards,
 
Upvote 0
Top