Android Manual recycle Bitmap, triggering canvas:trying to use a recycled bitmap processing

Source: Internet
Author: User

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 errors, 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 images before loading Bitmap, this time caused by the following error:

Java code
  1. Java.lang.RuntimeException:Canvas:trying to use a recycled bitmap android.graphics.Bitmap@41de4380
  2. At android.graphics.Canvas.throwIfRecycled (Canvas.java:1026)
  3. At Android.graphics.Canvas.drawBitmap (Canvas.java:1127)
  4. At Android.graphics.drawable.BitmapDrawable.draw (Bitmapdrawable.java:393)
  5. At Android.widget.ImageView.onDraw (Imageview.java:961)
  6. At Android.view.View.draw (View.java:13458)
  7. At Android.view.View.draw (View.java:13342)
  8. At Android.view.ViewGroup.drawChild (Viewgroup.java:2929)
  9. At Android.view.ViewGroup.dispatchDraw (Viewgroup.java:2799)
  10. At Android.view.View.draw (View.java:13461)
  11. At Android.view.View.draw (View.java:13342)

Picture Manual Recycling part of the code:

Java code
    1. Bitmap Removebitmap = Softreference.get ();
    2. if (removebitmap! = null &&!removebitmap.isrecycled ()) {
    3. Removebitmap.recycle (); //The above exception caused by this sentence
    4. Removebitmap = null;
    5. }

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 the 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 the adapter of the GetView () method to capture, the time is late, Not captured. 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

Java code
  1. Import Android.content.Context;
  2. Import Android.graphics.Canvas;
  3. Import Android.util.AttributeSet;
  4. Import Android.widget.ImageView;
  5. /**
  6. * Rewrite ImageView to avoid referencing bitmap exceptions that have been reclaimed
  7. *
  8. * @author Zwn
  9. *
  10. */
  11. Public class Myimageview extends ImageView {
  12. Public Myimageview (context context, AttributeSet attrs) {
  13. Super (context, attrs);
  14. }
  15. @Override
  16. protected void OnDraw (canvas canvas) {
  17. try {
  18. Super.ondraw (canvas);
  19. } catch (Exception e) {
  20. System.out
  21. . println ("Myimageview, OnDraw () canvas:trying to use a recycled bitmap");
  22. }
  23. }
  24. }

Android Manual recycle Bitmap, triggering canvas:trying to use a recycled bitmap processing

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.