The size of the image in android = The total number of images * Each image occupies the size.
Android saves picture pixel information using ARGB, meaning that each pixel occupies 4 bytes.
For images with a resolution of 2400*3200, loading to Android requires 2400*3200*4=30720000 bytes, requires 30000Kb, requires 29.30Mb
The image of this resolution is loaded directly into memory and easily oom.
WORKAROUND: Zoom the resolution of the image to the resolution size of the phone screen so that no oom will occur
1. Get to the Android.view.Display object by Getwindowmanager (). Getdefaultdisplay () method
2. Obtain the width and height of the screen resolution of the phone through the Getrealsize () method of the Android.view.Display object
3. Load the picture through Android.graphics.BitmapFactory's DecodeFile (String pathName, Options opts) method, the first parameter is the path of the picture, The second parameter is the Android.graphics.BitmapFactory.Options object, and by setting the options object's parameter Injustdecodebounds = True, you can do so without loading the picture size. Gets the width of the picture resolution
4. Divide the resolution width and high resolution of the image by the width and height of the screen resolution, get two scale, take a larger scale to scale the picture (note: The scale is better than 1, less than 1, you do not need to scale the direct loading on it)
5. Set the Options object's parameter insamplesize to scale, injustdecodebounds to false, and then use Android.graphics.BitmapFactory's DecodeFile ( String pathName, Options opts) method to load the picture, get to the object to be scaled bitmap (Injustdecodebounds is True when the bitmap object is not returned)
6. Process the Bitmap object.
1 Importandroid.app.Activity;2 ImportAndroid.graphics.Bitmap;3 Importandroid.graphics.BitmapFactory;4 Importandroid.graphics.BitmapFactory.Options;5 ImportAndroid.graphics.Point;6 ImportAndroid.os.Bundle;7 ImportAndroid.view.Display;8 ImportAndroid.view.View;9 ImportAndroid.widget.ImageView;Ten One Public classMainactivityextendsActivity { A - @Override - protected voidonCreate (Bundle savedinstancestate) { the Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); - } - + Public voidClick (View v) { -Options opt =NewOptions (); +Opt.injustdecodebounds =true; ABitmapfactory.decodefile ("Sdcard/dog.jpg", opt); at intImageHeight =Opt.outheight; - intImageWidth =Opt.outwidth; - -Display display =Getwindowmanager (). Getdefaultdisplay (); -Point point =NewPoint (); - //This method is obsolete and is replaced with the Getrealsize () method. You can also use GetSize (), but you can't get to the exact resolution in //int screenheight = Display.getheight (); - //int screenwidth = Display.getwidth (); to + display.getrealsize (point); - intScreenHeight =Point.y; the intScreenWidth =Point.x; * $ intScale = 1;Panax Notoginseng intScaleWidth = imagewidth/ScreenWidth; - intScaleheigh = imageheight/ScreenHeight; the if(ScaleWidth >= scaleheigh && scalewidth > 1) { +Scale =ScaleWidth; A}Else if(ScaleWidth < Scaleheigh && Scaleheigh > 1) { theScale =Scaleheigh; + } - $Opt.insamplesize =Scale ; $Opt.injustdecodebounds =false; - -Bitmap BM = bitmapfactory.decodefile ("Sdcard/dog.jpg", opt); theImageView IV =(ImageView) Findviewbyid (R.ID.IV); - Iv.setimagebitmap (BM);Wuyi } the -}
Android Development load Large resolution picture