Android有效解决加载大图片时内存溢出的问题(国外英文资料)
Android有效解决加载大图片时内存溢出的问题(国外英文资料)
Android effectively solves the problem of memory leaks when loading large images
Blog classification: Android
Android game virtual machine algorithm JNI
Try not to use setImageBitmap or setImageResource or bitmapfactors.decoderesource to set up a large image,
Because these functions are finished by decode, they end up using the createBitmap of the Java layer, which consumes more memory.
So, instead of using bitmapfactor.decodestream, we created a bitmap, and then we set it to the source of the ImageView,
The biggest secret of decodeStream is its direct call to JNI > > nativeDecodeAsset () to complete decode,
There is no need to use the createBitmap of the Java layer to save the Java layer space.
If you add the Config parameter to the image at the time of reading, you can effectively reduce the amount of Memory that is loaded in order to effectively block out the out of Memory exception
In addition, the decodeStream takes the image directly to read the bytecode and does not automatically adapt to the various resolutions of the machine,
After using the decodeStream, you need to configure the corresponding image resources in the hdpi and the mdpi, ldpi,
Otherwise, it's the same size on different resolution machines (the number of pixels), and the size of the display is incorrect.
In addition, the following ways are also helpful:
1.inputstream is this.getresources (R.d rawable.
Bitmapfactor.options Options = new bitmapfactor.options ()
Options. InJustDecodeBounds = false;
Options. InSampleSize = 10; / / width, hight is the original one
Bitmap BTP = bitmapfactory.decodestream (is, null, options);
If (!)
The memory used by the BMP. Recycle ()/recycle image
The system.gc () / / warning system is recovered in time
}
Here's a way:
Java code
/ * * 1.
* read pictures of local resources in the most convenient way
* @param context
* @param resId
* @return
6. * /
The public static Bitmap readBitMap (Context Context, int resId)
8 bitmapfactor.optio