Experience on Memory Optimization for Android Images

Source: Internet
Author: User

1. Convert the image into a thumbnail and load it again:

 BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 2; Bitmap img = BitmapFactory.decodeFile("/sdcard/1.png", options); 

This code is used to read the thumbnail of 1.png. The length and width are only 1/2 of the original image. The image size is reduced, and the memory occupied is naturally reduced. The disadvantage of this is that the image quality is deteriorated. The larger the value of insamplesize, the worse the image quality. Because mobile phone manufacturers have different Image Scaling algorithms, the image quality on different mobile phones may be different. I have encountered a situation where the quality is acceptable after the moto mobile phone is scaled. Samsung mobile phones have the same scaling ratio, but the quality is much worse.


2. Use the arbg_4444 color mode to load the image:

There are four types of Android:

Alpha_8: each pixel occupies 1 byte of memory

Argb_4444: each pixel occupies 2 bytes of memory

Argb_8888: each pixel occupies 4 bytes of memory

Rgb_565: each pixel occupies 2 bytes of memory

The default color mode for Android is argb_8888. This color mode features the most delicate colors and the highest display quality. However, the memory is also the largest.

 BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.ARGB_4444;     Bitmap img = BitmapFactory.decodeFile("/sdcard/1.png", options); 

The preceding code is used to read 1.png in argb_4444 mode. Although the memory reduction is not as obvious as the first method, most images cannot see any difference with the argb_8888 mode. However, when reading images with gradient effects, there may be color bars. In addition, it will affect the special effect processing of the image.

3. Call the recycle () method of the image:

This is not actually a way to actually reduce the image memory. The main purpose is to mark the image object to facilitate the collection of local data of the image object. The local data of the image object occupies the largest memory and is calculated separately from the Java memory of the program. Therefore, Java heap is often enough, and the image has an outofmemoryerror. This method can be called when the image is not in use, effectively reducing the peak value of the local data of the image, thus reducing the probability of outofmemoryerror. However, the image object that calls recycle () is in the "obsolete" state, which may cause a program error. Therefore, this method is not recommended when it cannot guarantee that the image object will never be called again. Note that setimagebitmap (Bitmap
IMG) the image object allocated to the control by the method may be called by the system library, resulting in a program error.

 

For example, on a Xiaomi phone:

Bitmap old = img;img = Bitmap.createBitmap(img, 0, 0, img.getWidth(), img.getHeight(), matrix, true);old.recycle();

This code will cause the bitmap object to be recycle. This code runs properly on Samsung, Moto, HTC, Huawei, and other vendors. Though it is caused by different implementation mechanisms of the system customized by the manufacturer. But it is indeed a matter of attention. 4. How to change the color mode of an image amplified by a matrix object: Although using a matrix object to enlarge an image will certainly consume more memory, sometimes it also has to be done. The enlarged image uses the argb_8888 color mode, even if the original image is in the argb_4444 color mode, and there is no way to directly specify the color mode when you zoom in. You can change the image color mode in the following ways.

Matrix matrix = new matrix (); float newwidth = 200; // float newheight = 300; // The length of the enlarged image matrix. postscale (newwidth/IMG. getwidth (), newheight/IMG. getheight (); bitmap img1 = bitmap. createbitmap (IMG, 0, 0, IMG. getwidth (), IMG. getheight (), matrix, true); // obtain the enlarged image img2 = img1.copy (bitmap. config. argb_4444, false); // obtain the image IMG = NULL in the argb_4444 color mode; img1 = NULL;

Here an additional image object img1 is generated compared to the original image. However, the system automatically recycles img1, so the actual memory is reduced.

 

In summary, the image is read in the thumbnail mode and the memory occupied by each pixel in the image is reduced. Although these two methods are effective, they also have their own drawbacks. In actual development, it should be used as appropriate. The most important way is to avoid the generation of junk objects. For example, in the use of listview, reuse convertview. If you use asynctask to load images, you must promptly set the referenced imageview object to null. Because asynctask is implemented by a thread pool, the referenced object may have a long life cycle, and GC cannot be released. I still believe in the memory recycle mechanism of Android. Although recycle is effective to some extent, I always feel that it is not in line with the Java memory recycle principle. (The last sentence is completely confusing)

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.