1. In the Android software development process, image processing is often encountered. When the picture is converted to bitmap, because the size of the picture is not the same, when the big picture will be more than memory problems, in order to solve this problem the Android API provides bitmapfactory.options this class.
2. Because of the restrictions on the use of Android images, if the load of a few megabytes of large pictures will overflow memory. Bitmap loads all the pixels (that is, the long x-width) of the picture into memory, and if the image resolution is too large, it directly results in a memory oom, and only when bitmapfactory loads the picture uses the bitmapfactory.options to configure the related parameters to reduce the pixels that are loaded.
3. Bitmapfactory.options Related parameter detailed:
(1). Options.inpreferredconfig value to reduce memory consumption.
For example: The default value argb_8888 changed to rgb_565, saving half of the memory.
(2). Set the options.insamplesize scaling ratio to compress large pictures.
(3). Set options.inpurgeable and ininputshareable: Allow the system to reclaim memory in time.
A:inpurgeable: When set to true, indicates that the system can be recycled when it is low on memory, and when set to false, it means that it cannot be reclaimed.
B:ininputshareable: Sets whether a deep copy is used in conjunction with inpurgeable, and inpurgeable is false when the parameter is meaningless.
(4). Use Decodestream instead of other methods.
Decoderesource,setimageresource,setimagebitmap and Other methods
4. Code section:
public static Bitmap getbitmapfromfile (file file, int width, int height) {Bitmapfactory.options opts = null; if (null!= file && file.exists ()) {if (width > 0 && height > 0) {opts = new Bitmap
Factory.options ();
It simply returns the width and height of the picture and does not return a Bitmap object opts.injustdecodebounds = true;
The information is not kept inside the bitmap, but is kept inside the options Bitmapfactory.decodefile (File.getpath (), opts);
Calculate picture Scaling Final int minsidelength = math.min (width, height); The thumbnail size is one of a fraction of the size of the original picture.
According to the business needs to do.
Opts.insamplesize = Computesamplesize (opts, minsidelength, Width * height);
Re-read the picture, note that at this point the options.injustdecodebounds has been set back to false opts.injustdecodebounds = false;
Sets whether a deep copy is used in conjunction with inpurgeable opts.ininputshareable = true;
When set to True, indicates that the system can be recycled when it is low on memory, and when set to False, it indicates that it cannot be reclaimed.
Opts.inpurgeable = true;
try {return Bitmapfactory.decodefile (File.getpath (), opts); catch (OutOfMemoryError e) {e.printstacktrace ();
} return null;
}