In Android you want to load a large picture into memory if you do this in the following way:
Bitmap bitmap= bitmapfactory.decodefile ("/sdcard/a.jpg"); Iv.setimagebitmap (Bitmap);
The memory overflow exception is thrown caused By:java.lang.OutOfMemoryError
The right approach should be this:
Public classMainactivityextendsActivity {PrivateImageView IV; Private intWindowHeight; Private intWindowWidth; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); IV=(ImageView) Findviewbyid (R.ID.IV); WindowManager win=(WindowManager) Getsystemservice (Window_service); WindowHeight=Win.getdefaultdisplay (). GetHeight (); WindowWidth=Win.getdefaultdisplay (). GetWidth (); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; } Public voidload (view view) {/*Bitmap bitmap= bitmapfactory.decodefile ("/sdcard/a.jpg"); Iv.setimagebitmap (bitmap);*/ //configuration of picture resolutionBitmapfactory.options Options =NewOptions (); //not to really parse the picture, just get the picture of the head information wide, highOptions.injustdecodebounds =true; Bitmapfactory.decodefile ("/sdcard/a.jpg", Options); intImageHeight =Options.outheight; intImageWidth =Options.outwidth; //Calculating the zoom ratio intScaleX = imagewidth/WindowWidth; intScaleY = imageheight/WindowHeight; intScale = 1; if(ScaleX > ScaleY & ScaleY >= 1) { scale=ScaleX; }Else if(ScaleY > ScaleX & ScaleX >= 1) { scale=ScaleY; } //really parse the pictureoptions.injustdecodebounds=false; //set the sample rateOptions.insamplesize=Scale ; Bitmap Bitmap=bitmapfactory.decodefile ("/sdcard/a.jpg", Options); Iv.setimagebitmap (bitmap); }}
In this case, the large-resolution picture is reduced to a certain scale and then loaded into memory, there will be no memory overflow phenomenon.
Reprint Please specify source: http://www.cnblogs.com/yydcdut/p/3744876.html
Reference: http://blog.csdn.net/zhong1113/article/details/23824933