Android--the way to load large images

Source: Internet
Author: User

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

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.