In practice, sometimes we need to load a large picture into memory from the SD card, but if your phone storage is very small, if you put this big picture directly into the content will cause the allocation failure and other abnormal problems. So we need a way to load the big picture into memory without the exception.
How can I load a large picture into memory without a memory overflow?
The Android system provides a way to load a picture by displaying a scale, which returns a smaller picture than the original.
1: First to get the picture parameters, the Android system provides a bitmapfactory this class
You can create a bitmap object that can obtain a picture resource from a different source.
Bitmapfactory.decoderesource (res, id)
Use the code above to get a bitmap object. The second parameter of the Bitmapfactory.decoderesource method can be used to get the parameters of a picture
Bitmapfactory.decoderesource (Getresources (), R.drawable.dog, opts);
2:options is a class that encapsulates the parameters of a picture
/*injustdecodebounds equals true: Represents the return of NULL, not bitmap object, but can get the source file parameter condition */ opts.injustdecodebounds = true;
3: Get the width and height of the screen
Gets the width and height of the display DP = Getwindowmanager (). Getdefaultdisplay (); int screenwidth = Dp.getwidth (); int screenheight = Dp.getheight ();
4: Calculate the zoom ratio:
/* Calculate the magnification of the picture */ int scale = 1; int scalewidth = imagewidth/screenwidth; int scaleheight = imageheight/screenheight; if (scalewidth >= scaleheight && scalewidth >= 1) {scale = ScaleWidth; } else if (ScaleHeight > ScaleWidth && scaleheight >= 1) {scale = ScaleHeight;}
5: Get the Bitmap object again, in a proportional way to get
/* Set the zoom ratio of the picture */ opts.insamplesize = scale;/* If greater than 1, request decoder to sample the original, return a smaller image*/ opts.injustdecodebounds = False ; Bitmap Bitmap = Bitmapfactory.decoderesource (Getresources (), R.drawable.dog, opts); /* Display picture */ ImageView IView = (ImageView) Findviewbyid (R.ID.IV); Iview.setimagebitmap (bitmap);
The entire code:
public class Mainactivity extends Activity { @Override protected void OnCreate (Bundle savedinstancestate) { super.oncreate ( savedinstancestate); Setcontentview (R.layout.activity_main); Button button = (Button) Findviewbyid (R.id.button); Button.setonclicklistener (new Onclicklistener () { @Override public void OnClick (View arg0) {& nbsp; //TODO auto-generated method stub log.i ("mainactivity", "onclick"); /* First parse this very big picture, get picture parameter situation */ options opts = new Options (); & nbsp /*injustdecodebounds equals true: Represents the return of NULL, It is not a bitmap object, but it can get the parameter of the source file */ Opts.injustdecodebounds = true; //bitmapfactory This class is a bitmap object that is created by a different source bitmapfactory.decoderesource ( Getresources (), R.drawable.dog, opts); //Get image width and height int imagewidth = opts.outwidth; int imageheight = opts.outheight; //get screen width and height display dp = Getwindowmanager (). Getdefaultdisplay (); & nbsp; int screenwidth = Dp.getwidth (); int screenheight = Dp.getheight (); & nbsp /* Calculate the scale of the picture */ int scale = 1; & nbsp; int scalewidth = imagewidth/screenwidth; & nbsp; int scaleheight = imageheight/screenheight; if (scalewidth >= scaleheight && scalewidth >= 1) { scale = scalewidth; } else if (ScaleHeight > ScaleWidth && scaleheight >= 1) { & nbsp; scale = scaleheight; } /* Set the scale of the picture */ Opts.insamplesize = scale;/* If greater than 1, request decoder to sample the original, return a smaller image*/ opts.injustdecodebounds = false; bitmap Bitmap = Bitmapfactory.decoderesource (Getresources (), R.drawable.dog, opts);   /* Show pictures */ imageview IView = (ImageView) Findviewbyid (R.ID.IV); & nbsp; iview.setimagebitmap (bitmap); } }); }}
:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Learning Multimedia Development---loading big picture