I. Recycle of Bitmap

Source: Internet
Author: User

Although android has its own garbage collection mechanism, it depends on the situation if we want to call Recycle by ourselves. It is assumed that only a few images are used. However, if there are a large number of bitmaps that require garbage collection, the number of times that need to be done for garbage collection will also occur more frequently, which will load system resources. Therefore, it is better to try to release the Recycle by yourself.

It is worth considering how to use the recycle () method and when to try recycle. No errors:

05-15 08:31:00.117: E/AndroidRuntime(513): java.lang.RuntimeException: Canvas: trying to use a recycled bitmap [email protected]
Java execution exception: Try to use an image that has been recycled


Only when you confirm that you are not using this bitmap, you can choose to call the recycle () method to release it.

The recycle () call encountered an exception when I was trying a small notebook today. The reason is:

In the XML file, imageview has two methods to set a display image:

 android:background="@drawable/aa" android:src="@drawable/aa"

At the beginning, I used background and set it in the program.

// Obtain the currently displayed image bitmap bitmap1 = (bitmapdrawable) imageview. getbackground ()). getbitmap (); bitmap bitmap2 = bitmap. createbitmap (bitmap1, 0, 0, bitmap1.getwidth (), bitmap1.getheight (), matrix, true); // if the image has not been recycled, forcibly recycle if (! Bitmap1.isrecycled () {bitmap1.recycle ();} // create an image imageview. setimagebitmap (bitmap2) based on the original bitmap and matrix );

The imageview. setimagebitmap () method sets the SRC image instead of the background. However, I recycled the background image. In this way, an exception occurs.

So change it to the following code to call the setbackground () method.

// Obtain the currently displayed image bitmap bitmap1 = (bitmapdrawable) imageview. getbackground ()). getbitmap (); bitmap bitmap2 = bitmap. createbitmap (bitmap1, 0, 0, bitmap1.getwidth (), bitmap1.getheight (), matrix, true); // if the image has not been recycled, forcibly recycle if (! Bitmap1.isrecycled () {bitmap1.recycle ();} // create a new image based on the original bitmap and matrix // imageview. setimagebitmap (bitmap2); imageview. setbackgrounddrawable (New bitmapdrawable (bitmap2 ));

Therefore, you must pay attention to the source of the imageview image and then perform the corresponding recycle.


I. Recycle of Bitmap

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.