In the development of Android, in the ListView or GridView need to load a large number of images, in order to avoid loading too many pictures caused OutOfMemory error, set up a picture cache list map<string , softreference<bitmap>> Imagecache , and maintenance, in the picture loading to a certain amount of time, the manual collection of the image before loading the Bitmap, this time caused by the following error:
java.lang.RuntimeException:Canvas:trying to use a recycled bitmap [e-mail protected] at android.graphics.Canvas.th Rowifrecycled (Canvas.java:1026) at Android.graphics.Canvas.drawBitmap (Canvas.java:1127) at Android.graphics.drawable.BitmapDrawable.draw (Bitmapdrawable.java:393) at Android.widget.ImageView.onDraw (Imageview.java:961) at Android.view.View.draw (View.java:13458) at Android.view.View.draw (View.java:13342) at Android.view.ViewGroup.drawChild (Viewgroup.java:2929) at Android.view.ViewGroup.dispatchDraw (Viewgroup.java:2799) at Android.view.View.draw (View.java:13461) at Android.view.View.draw (View.java:13342)
Picture Manual Recycling part of the code:
Bitmap removebitmap = Softreference.get (); if (Removebitmap! = null &&! removebitmap.isrecycled ()) {removebitmap.recycle (); // Removebitmap = null ;}
There are a lot of people on the Internet that should remove recycle () , the individual thinks that the removal will cause the memory to continue to grow, although the bitmap is set to NULL, but the system does not really recycle it, still occupy memory, that is, call the System.GC () after forced back, memory is still not down, if you rely on memory to reach the system itself to recycle, personally feel too late, has been the impact of the application, the application should be compared card, so it is agreed to add bitmap.recycle () , but will cause canvas:trying to use a recycled bitmap anomaly, troubled for a long time, began to try to solve the problem from other aspects, that is, the exception should be able to capture, but in adapter GetView () method, the time is too late to capture. Now change to capture in ImageView 's OnDraw () , the above exception can be captured.
Workaround (Inherit ImageView override OnDraw () method, catch Exception):
In the rewrite OnDraw () method, nothing is done, just add an exception catch to catch the error above
ImportAndroid.content.Context;ImportAndroid.graphics.Canvas;ImportAndroid.util.AttributeSet;ImportAndroid.widget.ImageView;/*** Override ImageView to avoid referencing reclaimed bitmap exceptions * *@authorZwn **/ Public classMyimageviewextendsImageView { PublicMyimageview (Context context, AttributeSet attrs) {Super(context, attrs); } @Overrideprotected voidOnDraw (canvas canvas) {Try { Super. OnDraw (canvas); } Catch(Exception e) {System.out. println ("Myimageview-OnDraw () canvas:trying to use a recycled bitmap"); } }}
Original address: http://zwnjava.iteye.com/blog/1901880
"Go" Android manual recycle bitmap, triggering canvas:trying to use a recycled bitmap processing