Solve memory overflow problems when Android loads pictures

Source: Internet
Author: User

Try not to use Setimagebitmap or Setimageresource or Bitmapfactory.decoderesource to set a large image, because these functions are completed decode, and ultimately through the Java layer of CreateBitmap to complete, you need to To consume more memory.

Therefore, instead of using the Bitmapfactory.decodestream method first to create a bitmap, and then set it to ImageView Source,decodestream the biggest secret is its direct call jni>> Nativedecodeasset () to complete the decode, eliminating the need to use the Java layer CreateBitmap, thus saving the Java layer of space.
If the config parameter is added to the read, it can be used to reduce the loaded memory effectively, thereby effectively blocking the throw out of
In addition, Decodestream directly take the picture to read the bytecode, not according to the machine's various resolutions to automatically adapt, after using the Decodestream, you need to configure the corresponding image resources in hdpi and mdpi,ldpi. Otherwise the same size (number of pixels) is the same on different resolution machines, and the displayed size is wrong.

In addition, the following ways can help:

    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 ()//Recover the memory occupied by the picture
      System.GC ()//Alarm system timely recovery
      }

Solve memory overflow problems when Android loads pictures

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.