Android OOM Solution

Source: Internet
Author: User

Do not use setImageBitmap, setImageResource, or BitmapFactory. decodeResource to set a large image,
Because after decode is completed, these functions are ultimately completed through the createBitmap on the java layer, and more memory is required.


Therefore, use the BitmapFactory. decodeStream method to create a bitmap and set it to the source of ImageView,
The biggest secret of decodeStream is that it directly calls JNI> nativeDecodeAsset () to complete decode,
You no longer need to use the createBitmap on the java layer, thus saving the java Layer Space.
If the configuration parameter of the part is added during reading, the load Memory can be effectively reduced to prevent the throwing out of Memory exception.
In addition, decodeStream reads bytecode directly from images and does not automatically adapt to various machine resolutions,
After decodeStream is used, you need to configure the corresponding image resources in hdpi, mdpi, and ldpi,
Otherwise, the machine with different resolutions is of the same size (number of pixels), and the displayed size is incorrect.


In addition, the following methods are also helpful:
1. InputStream is = this. getResources (). openRawResource (R. drawable. pic1 );
BitmapFactory. Options options = new BitmapFactory. Options ();
Options. inJustDecodeBounds = false;
Options. inSampleSize = 10; // width, hight is set to the original 10-minute-one
Bitmap btp = BitmapFactory. decodeStream (is, null, options );
2. if (! Bmp. isRecycle ()){
Bmp. recycle () // reclaim the memory occupied by the image
System. gc () // remind the system to recycle it in time
}


Here is a method:


Java code


1 ./**
2. * read images of local resources in the most memory-saving manner
3. * @ param context
4. * @ param resId
5. * @ return
6 .*/
7. public static Bitmap readBitMap (Context context, int resId ){
8. BitmapFactory. Options opt = new BitmapFactory. Options ();
9. opt. inPreferredConfig = Bitmap. Config. RGB_565;
10. opt. inPurgeable = true;
11. opt. ininputretriable = true;
12. // obtain the resource Image
13. InputStream is = context. getResources (). openRawResource (resId );
14. return BitmapFactory. decodeStream (is, null, opt );
15 .}




========================================================== ==========================================================
Android memory overflow Solution


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.