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