Bitmapfactory.options details how to get the size of the picture encountered a larger picture, encountered the problem of Oom (out of Memory)

Source: Internet
Author: User

Let's go back to our topic: How do I get the size of a picture?

The idea is simple:

First we turn this image into bitmap, and then use the bitmap getwidth () and the GetHeight () method to take the picture's width high.

New problems have come again, when the breakthrough through the Bitmapfactory.decodefile (Stringpath) method to turn into bitmap, encountered a larger picture, we often encounter oom (out of Memory) problem. How to avoid it?

This is the kind of bitmapfactory.options we mentioned above.

Bitmapfactory.options This class, there is a field called Injustdecodebounds. The description of this member in the SDK is this: If set to True, the decoder would return null (no bitmap), but the ... That is, if we set it to true, then Bitmapfactory.decodefile (Stringpath, Options opt) will not really return a bitmap to you, it will simply take it wide and high back to you, This will not take up too much memory, and it will not happen so frequently with oom. The sample code is as follows:

Bitmapfactory.options Options = Newbitmapfactory.options ();

Options.injustdecodebounds = true;

Bitmap bmp = Bitmapfactory.decodefile (path,options);

/* The BMP returned here is NULL */

Copy Code

After this code, Options.outwidth and Options.outheight are the width and height we want.

With wide, high information, how do we get thumbnails of the size of a picture without distortion? For example, we need to get a thumbnail with a width of 200 if the image is not deformed. So we need to figure out what the height of the image is after scaling.

/* Calculate the height of the picture */

/* Here's the idea, if you need more precision to ensure that the picture does not deform, you need to do a math operation * *

int height = options.outheight * 200/options.outwidth;

Options.outwidth = 200;

Options.outheight = height;

/* This will really return a bitmap to you */

Options.injustdecodebounds = false;

Bitmap bmp = Bitmapfactory.decodefile (path,options);

Image.setimagebitmap (BMP);

Copy Code

This way although we can get the imageview of our desired size but in the execution of Bitmapfactory.decodefile (path,options), there is no memory savings. To conserve memory, you also need to use the insamplesize member variable in the Bitmapfactory.options class. we can calculate this value according to the actual width of the picture and the width of the height we expect.

Insamplesize = options.outwidth/200;

In addition, in order to save memory we can also use the following several fields:

Options.inpreferredconfig =bitmap.config.argb_4444; Default is Bitmap.Config.ARGB_8888

/* The following two fields need to be combined with */

Options.inpurgeable = true;

Options.ininputshareable = true;


1.bitmapfactory.decodefile (String path) turns into bitmap,bitmap too large and can also cause oom

2.bitmapfactory.option parameter, the returned bitmap is empty and we can get bitmap width and height. Calculate the width and height we want by calculating the method

By setting the Injustdecodebounds

3. Other settings, options.inpreferendconfig=bitmap.config.argb_4444


Bitmapfactory.options details how to get the size of the picture encountered a larger picture, encountered the problem of Oom (out of Memory)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.