Cainiao can also solve the OOM problem in Android

Source: Internet
Author: User

As long as you remember the following principles, the OOM problem for processing images in Android is absolutely easy:

1. Large images must be compressed in proportion before they are displayed. to exit the current activity, they must be recycled.

 
Public static bitmap decodesampledbitmapfromresource (Resources res, int resid, int reqwidth, int reqheight) {// first decode with injustdecodebounds = true to check dimensions final bitmapfactory. options = new bitmapfactory. options (); options. injustdecodebounds = true; bitmapfactory. decoderesource (Res, resid, options); // calculate insamplesize options. insamplesize = calculateinsamplesize (options, reqwidth, reqheight); // decode bitmap with insamplesize set options. injustdecodebounds = false; return bitmapfactory. decoderesource (Res, resid, options );}

The insamplesize can be determined based on your actual situation.

If (Bitmap! = NULL &&! Bitmap. isrecycled () {bitmap. Recycle (); bitmap = NULL ;}

2. Large images (30 ~ 50 k) can be directly displayed, exit the current activity to immediately Recycle

 
If (Bitmap! = NULL &&! Bitmap. isrecycled () {bitmap. Recycle (); bitmap = NULL ;}

3. A large number of small images or images of different sizes are to be displayed. Please refer to my other LRU article.AlgorithmCached image: http://blog.csdn.net/androidzhaoxiaogang/article/details/8211649

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.