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