Doubts about canvas:trying to use a recycled bitmap android.graphics

Source: Internet
Author: User


==================== Problem Description ====================
Because Viewpager picture memory overflow problem, have to consider manually free memory, but out of the problem I do not understand.

My idea is to create a map, and then use the parameter arg0 in the Instantiateitem as the key, bitmap as a value, when I remove the view in the Destroyitem, I take the bitmap that is no longer used to recycle
This is a map of bitmap.
Public Hashmap<integer, softreference<bitmap>> Cachebit;

The approximate relevant code in adapter is this:

This is the recycle code in Destroyitem.
This is recycled, and when we swipe left and right, we recycle the early pictures.
public void Destroyitem (View arg0, int arg1, Object arg2) {
TODO auto-generated Method Stub
View view = (view) arg2;
((Viewpager) arg0). Removeview (view);
Code to recycle pictures
if (Cachebit.containskey (arg1)) {
try {
Bitmap BM = Cachebit.get (arg1). get ();
if (null! = BM &&!bm.isrecycled ()) {
Bm.recycle ();
BM = NULL;
}
Remind the system to recycle pictures
System.GC ();
} catch (Exception e) {
Todo:handle exception
System.out.println ("Picture has been recycled");
}

Cachebit.remove (arg1);
}
}

And then this is the page that loads the view, and in this way I'll bitmap in the map for easy recycling:
Public Object Instantiateitem (View arg0, int arg1) {
TODO auto-generated Method Stub
This section is not related to the code
View view;
Layoutinflater Inflater = layoutinflater.from (context);
View = Inflater.inflate (R.layout.headingline_slider, NULL);
relative = (relativelayout) View.findviewbyid (r.id.bg);

Here is the asynchronous loading of the picture
According to the image URL to find the memory cache there is no corresponding bitmap object, and pass the callback method, if not, then wait for the download to complete the callback
Bitmap Bitmap = Imageloader.loadbitmap (relative, List.get (arg1), new Imagecallback () {
@Override
public void Imageload (relativelayout relative, Bitmap Bitmap) {
TODO auto-generated Method Stub
Relative.setbackgrounddrawable (New bitmapdrawable (bitmap));
}
});
if (bitmap = = null) {
Relative.setbackgrounddrawable (New Bitmapdrawable (BT));
} else {
Relative.setbackgrounddrawable (New bitmapdrawable (bitmap));
}

Load the bitmap into a map for easy reuse and recycling
Cachebit.put (arg1, New softreference<bitmap> (Bitmap));

((Viewpager) arg0). AddView (view);
return view;
}



Logically I do not know where the error, but the result of the execution of the slide a few times will be reported an exception, said I tried to use the recovered pictures, do not understand
Canvas:trying to use a recycled bitmap [email protected]
==================== Solution 1====================
If you have the same URL you want to control the reference count of the picture only for 0 you can recycle.
==================== Solution 2====================
Bitmap Bitmap = Imageloader.loadbitmap (relative, List.get (arg1), new Imagecallback () {
@Override
public void Imageload (relativelayout relative, Bitmap Bitmap) {
TODO auto-generated Method Stub
Relative.setbackgrounddrawable (New bitmapdrawable (bitmap));
}
});
if (bitmap = = null) {
Relative.setbackgrounddrawable (New Bitmapdrawable (BT));
} else {
Relative.setbackgrounddrawable (New bitmapdrawable (bitmap));
}

Load the bitmap into a map for easy reuse and recycling
Cachebit.put (Arg1, New softreference<bitmap> (Bitmap));

/
It is possible to cache the Bitmap=null in the caches.
At initialization time your bitmap must be null, but you still write this null to the cache




==================== Solution 3====================
According to the code, you should be using Imageloader, a third-party tool,
Basically the more perfect tool will provide the picture caching mechanism, and has a relatively perfect elimination algorithm.
When your item is destroyed, you recyle bitmap, but for Imageloader it does not know this picture recycle, it is possible the same URL he returned to this recycled picture.

You check it out.

Doubts about canvas:trying to use a recycled bitmap android.graphics

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.