The use of bitmapfactory.options in Android is when you load a picture, and you start by loading and using it.
How do I get the size of a picture?
First we turn the picture into a bitmap and then use the bitmap getwidth () and GetHeight () method to get the picture wide.
A new problem comes up, and when you go through the Bitmapfactory.decodefile (String path) method and turn the break into bitmap, we often encounter the problem of Oom (out of Memory) when we encounter larger pictures. How can we avoid it?
This uses the Bitmapfactory.options class that we mentioned above.
Bitmapfactory.options This class, there is a field called Injustdecodebounds. The description of this member in the SDK is as follows:
If set to True, the decoder would return null (no bitmap), but the "out ..."
That is to say, if we set it to true, then Bitmapfactory.decodefile (String path, Options opt) does not really return a bitmap to you, it will just take it wide and high to get back to you, This will not occupy too much memory, it will not be so frequent occurrence of oom.
The sample code is as follows:
Bitmapfactory.options Options = new Bitmapfactory.options ();
Options.injustdecodebounds = true;
Bitmap bmp = Bitmapfactory.decodefile (path, options);/* The BMP returned here is NULL */
Bitmapfactory.options Options = new Bitmapfactory.options ();
Options.injustdecodebounds = true;
Bitmap bmp = Bitmapfactory.decodefile (path, options);/* The BMP returned here is NULL */
After this code, Options.outwidth and Options.outheight are the widths and heights we want.
With wide, high information, how do we get a thumbnail of a given size in a picture without distorting it?
For example, we need to get a thumbnail with a width of 200 if the picture is not deformed.
So we need to figure out what the height of the picture is after zooming, the code is as follows
int height = options.outheight * 200/options.outwidth;
Options.outwidth = 200;
Options.outheight = height;
Options.injustdecodebounds = false;
Bitmap bmp = Bitmapfactory.decodefile (path, options);
Image.setimagebitmap (BMP);
int height = options.outheight * 200/options.outwidth;
Options.outwidth = 200;
Options.outheight = height;
Options.injustdecodebounds = false;
Bitmap bmp = Bitmapfactory.decodefile (path, options);
Image.setimagebitmap (BMP);
So that while we can get the size of the imageview we expect
But when executing bitmapfactory.decodefile (path, options), it does not save memory. To save memory, you need to use the Insamplesize member variable in the Bitmapfactory.options class.
We can calculate this value based on the actual width of the picture and the height of the width we expect.
Options.insamplesize = options.outwidth/200;
/* Picture width of the long way to reduce the multiple *
Options.insamplesize = options.outwidth/200;
* * Picture long width reduction multiple
In addition, in order to save memory we can also use the following several fields:
options.indither=false;/* does not perform image jitter processing * *
Options.inpreferredconfig=null; /* settings allow decoder to decode in the best way
/* The following two fields need to be combined use * *
Options.inpurgeable = true;
Options.ininputshareable = true;
Android Bitmapfactory.options optimization method to avoid memory overflow oom
Try not to use Setimagebitmap or Setimageresource or Bitmapfactory.decoderesource to set a larger picture,
Since these functions finish decode, they are ultimately done through the createbitmap of the Java layer, which consumes more memory.
Instead, use the Bitmapfactory.decodestream method first to create a bitmap and then set it to ImageView source.
Decodestream's biggest secret is that it calls Jni>>nativedecodeasset () directly to complete decode,
There is no need to use the Java layer's CreateBitmap, which saves the Java layer's space.
If the config parameter of the picture is added to the read, it can be used to reduce the loaded memory effectively, thus preventing the throw out of the memory exception
In addition, Decodestream directly to the image to read the bytecode, will not be based on the machine's various resolutions to automatically adapt,
After using Decodestream, you need to configure the corresponding picture resources in hdpi and mdpi,ldpi.
Otherwise, in different resolution machines are the same size (number of pixels), the display of the size is not correct.
In addition, the following ways are 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 very
Bitmap BTP =bitmapfactory.decodestream (is,null,options);
2. if (!bmp.isrecycle ()) {
Bmp.recycle ()//Reclaim the memory occupied by the picture
System.GC ()//reminding system to be recycled in time
}
Here's the previous method:
Java code
/**
* Picture of local resources in the most memory-saving way
* @param context
* @param resid
* @return/public
static Bitmap Readbitmap (context context, int resid) {
bitmapfactory.options opt = new bitmapfactory.options ();
opt.inpreferredconfig = Bitmap.Config.RGB_565;
opt.inpurgeable = true;
opt.ininputshareable = true;
//Get a picture
of the resource InputStream is = Context.getresources (). Openrawresource (RESID); Return
Bitmapfactory.decodestream (is,null,opt);
}
Optimizing heap memory allocations for Dalvik virtual machines
for the Android platform, the Dalvik used by its hosting layer JAVAVM from the present performance, there are still many places to optimize processing, such as we in the development of some large games or resource-consuming applications may consider manual interference GC processing, using The Settargetheaputilization method provided by the Dalvik.system.VMRuntime class can enhance the processing efficiency of the program heap memory. Of course, we can refer to the specific principles of open source Engineering, here we only say the use of the method: private final static floattarget_heap_utilization = 0.75f; Vmruntime.getruntime (). Settargetheaputilization (target_heap_utilization) can be invoked when the program is OnCreate. Can.
describes the memory algorithm for the picture occupancy process.
the underlying class for working with pictures in Android is bitmap, as the name suggests, bitmaps. The memory-hogging algorithm is as follows:
The width*height*config of the picture.
If config is set to argb_8888, the above config is 4. A picture of 480*320 occupies a memory that is 480*320*4 byte.
Someone said the concept of 8M, in fact, by default, the Android process memory footprint is 16M, because bitmap in addition to holding data in Java, the underlying C + + Skia graphics library will also hold a Skbitmap object, Therefore the general picture occupies the memory recommendation size should not exceed 8M. This can be adjusted to set the parameters when compiling the source code.