[Android] load large picture into memory

Source: Internet
Author: User

By default, the size of the heap memory allocated by the Android program is heep, and the VM above the virtual machine is set to its

A picture takes up memory, such as a picture of 1920*2560 pixels needed,1920*2560*3 At least these bytes of memory

Locate the ImageView Control Object

Call the decodefile (pathName) method of the bitmapfactory object to get a bitmap object, parameter:pathName is a String type of picture path

Import the picture into the sdcard directory of your phone

Call the setimagebitmap (bitemap) method of the ImageView object , Parameter:bitemap Object

A memory overflow error is reported at this time

We need to zoom in on the picture

The resolution of the phone such as:320*480 image resolution such as:2000*4000

Calculate the proportions separately,2000/320 4000/480, scale according to the large scale

Call the overloaded method bitmapfactory object's decodefile (pathname,opts), Parameters: path,Options Object

Gets the bitmapfactory.option object, via the New Options () method

Sets the properties of the Options object injustdecodebounds to ture, parsing only header information data

Gets the outheight property of the Options object, with a value of the height of the picture

Gets the outwidth property of the Options object, with a value of the width of the picture

Gets the windowmanager object, through the getsystemserivce () method, Parameter:window_service

Call the WindowManager object's getdefaultdisplay (). GetHeight ( ) or getwidth () method to obtain a wide height

Calculating the zoom ratio of width and height

Judging, when the ratio is greater than 1 , find the large value inside the wide height as the picture scale

After calculating the scale

Set The properties of the Options object injustdecodebounds to false, true parse picture

Set The sample rate property of the Options object insamplesize to the large scale calculated above

Call the overloaded method bitmapfactory object's decodefile (pathname,opts), get to Bitmap Object

Call the setimagebitmap (bitemap) method of the ImageView object , Parameter:bitemap Object

Exif is the header information of a picture file

Get exifinterface object, come out with new

Call the getattribute () method of the exifinterface object to get the picture information, parameters:tag

Exifinterface.tag_datetime Shooting Time

Exifinterface.tag_model Shooting Camera

Code:

 Packagecom.tsh.loadbigimg;Importjava.io.IOException;Importandroid.app.Activity;ImportAndroid.graphics.Bitmap;Importandroid.graphics.BitmapFactory;Importandroid.graphics.BitmapFactory.Options;ImportAndroid.media.ExifInterface;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.WindowManager;ImportAndroid.widget.ImageView; Public classMainactivityextendsActivity {PrivateImageView iv_img; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Iv_img=(ImageView) Findviewbyid (r.id.iv_img); }    //Load Large picture     Public voidload (View v) {Options opts=NewOptions (); Opts.injustdecodebounds=true; Bitmapfactory.decodefile ("/sdcard/a.jpg", opts); //the width and height of the picture        intImgwidth=Opts.outwidth; intimgheight=Opts.outheight; //the width and height of the screenWindowManager wm=(WindowManager) Getsystemservice (Window_service); intwindowheight=Wm.getdefaultdisplay (). GetWidth (); intWindowwidth=Wm.getdefaultdisplay (). GetWidth (); intscalex=imgwidth/WindowWidth; intscaley=imgheight/WindowHeight; System.out.println ("X scale:" +ScaleX); System.out.println ("Y scale:" +ScaleY); //Calculating the zoom ratio        intScale=1; if(scalex>scaley&&scaley>1) { scale=ScaleX; }        if(scaley>scalex&&scalex>1) { scale=ScaleY; } System.out.println ("Scale:" +Scale ); Opts.injustdecodebounds=false; Opts.insamplesize=Scale ; Bitmap Bitmap=bitmapfactory.decodefile ("/sdcard/a.jpg", opts);            Iv_img.setimagebitmap (bitmap); }    //Reading Information     Public voidRead (View v) {Try{exifinterface exif=NewExifinterface ("/sdcard/a.jpg"); String Date=Exif.getattribute (exifinterface.tag_datetime); String Model=Exif.getattribute (Exifinterface.tag_model); System.out.println ("Camera:" +model+ "; Time:" +date); } Catch(Exception e) {e.printstacktrace (); }            }}

[Android] load large picture into memory

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.