Bitmapfactory. Options: http://developer.android.com/reference/android/graphics/bitmapfactory.options.html;
The related information is as follows:
This table is extracted from the android SDK documentation. After a brief look, you can see what it means.
Next we will go back to our topic: how to get the image size?
The idea is simple:
First, convert the image to a bitmap, and then use the getwidth () and getheight () Methods of Bitmap to obtain the width and height of the image.
The new problem arises again. When bitmapfactory. decodefile (string path) is used to convert the method into bitmap, we often encounter out of memory (OOM) problems when encountering Larger images. How can we avoid it?
This uses the bitmapfactory. Options class we mentioned above.
Bitmapfactory. Options has a field called injustdecodebounds. The descriptions of this Member in the SDK are as follows:
If set to true, the decoder will 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 actually return a bitmap to you. It only returns its width and height to you, so that it will not occupy too much memory, oom will not happen so frequently.
The sample code is as follows:
Bitmapfactory. Options = new bitmapfactory. Options (); options. injustdecodebounds = true; bitmap BMP = bitmapfactory. decodefile (path, options);/* The returned BMP is null */
After this code, options. outwidth and options. outheight are the width and height we want.
With the width and height information, how can we obtain the thumbnail of the specified image size without deformation?
For example, we need to get a thumbnail with a width of 200 without deformation of the image.
Then we need to calculate the Image Height after scaling.
/* Calculate the image height * // * Here you need an idea. If you need higher accuracy to ensure that the image is not deformed, perform a mathematical operation on your own */INT Height = options. outheight * 200/options. outwidth; options. outwidth = 200; options. outheight = height;/* in this way, a bitmap is returned to you */options. injustdecodebounds = false; bitmap BMP = bitmapfactory. decodefile (path, options); image. setimagebitmap (BMP );
In this way, we can get the desired imageview size.
However, when bitmapfactory. decodefile (path, options); is executed, the memory is not saved. To save memory, you also need to use the insamplesize member variable in the bitmapfactory. Options class.
We can calculate this value based on the actual width and the expected width and height of the image.
inSampleSize = options.outWidth / 200;
In addition, the following fields can be used to save memory:
Options. inpreferredconfig = bitmap. config. argb_4444; // The default value is bitmap. config. the following two fields in argb_8888/* need to be combined to use */options. inpurgeable = true; options. ininputretriable = true;
References:
Bitmapfactory. Options