Android Load Picture Memory overflow problem Resolution _android

Source: Internet
Author: User

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;
 }
Related Article

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: info-contact@alibabacloud.com 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.