Resolves a memory overflow problem when loading images ImageView

Source: Internet
Author: User


The problem of memory overflow in picture loading is very common and the method of solving it is different.

The main reason is that ImageView takes up a lot of memory when it creates the image layer at the bottom.

Try not to use Setimagebitmap or Setimageresource or Bitmapfactory.decoderesource to set up a larger image directly using the picture path, as these functions are ultimately passed through the Java layer after the decode is completed CREATEBITM AP to complete and consumes more memory. Instead, use the Bitmapfactory.decodestream method first to create a bitmap, and then call the method above to set it to ImageView source. Decodestream's biggest secret is that it directly calls Jni>>nativedecodeasset () to complete the decode, eliminating the need to use the Java layer's createbitmap, thus saving the Java layer space. Here is the image loaded using InputStream

To make a long story short, write directly on your own method, good and bad, for reference only.

/**    * large picture processing mechanism    * UseBitmapDumpRImage    */   Public StaticBitmapBTP;Public static voidGetbitmapforimgresourse (Context Mcontext,intImgid,imageview Mimageview)throwsIOException {InputStream is = Mcontext.getresources (). Openrawresource (Imgid); Bitmapfactory.options Options =NewBitmapfactory.options (); Options.Injustdecodebounds=false; Options.Inpreferredconfig= Bitmap.config.rgb_565; Options.inpurgeable=true; Options.ininputshareable=true; Options.insamplesize=1;BTP= Bitmapfactory.Decodestream*I1,NULL, options); Mimageview.setimagebitmap (BTP);//Btp.recycle ();      Is.close (); }
Invocation mode
Utils. Getbitmapforimgresourse (mcontext, Object.getimgid (), Holder. mimg);
First parameter Context second parameter r.drawable.xx the third parameter  sets the ImageView of the picture
Let's use it to see if it's good.  


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Resolves a memory overflow problem when ImageView loading 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.