Android Load Large image

Source: Internet
Author: User

First step: Read the size and type of the picture

According to the different selection of the image data source bitmapfactory the corresponding static method, through the following method can be common image decoding to generate bitmap objects.
If the image data source is a binary stream, select the Bitmapfactory.decodebytearray () method,
If the picture is below a file path, select the Bitmapfactory.decodefile () method,
If the picture is in the internal Res folder of the app under the Bitmapfactory.decoderesource () method,
Note: It is common to call methods with the Bitmapfactory.options type parameter in the above three method signatures, by setting the Injustdecodebounds property of the Bitmapfactory.options object to True, This will not allocate memory for the decoded image, and it returns an empty bitmap object. However, this method can be used to obtain the width and height of the image and the format of the picture, in this way we can get the image format and size (width and height) before allocating memory to create bitmap, thus avoiding the occurrence of java.lang.OutOfMemory exception. Unless you are absolutely sure that the picture is within memory range, be sure to get its size and type before decoding the picture, code example:

1 New bitmapfactory.options (); 2 true ; 3 Bitmapfactory.decoderesource (Getresources (), r.id.myimage, options); 4 int imageheight = options.outheight; 5 int imagewidth = options.outwidth; 6 String imageType = Options.outmimetype;
Step two: Load the compressed version of the picture into memory.

In the first step we have got the size of the picture, and then we actually picture the actual image using memory, the application itself other also need to occupy memory, screen size, current device pixel, display the picture of the UI component of the width and high factor to decide whether to compress the picture if you want to compress the picture, you need to calculate the compression ratio of the picture The compression scale is then assigned to the Insamplesize property of the Bitmapfactory.options object, noting that the injustdecodebounds of the object before this bitmapfactory.options needs to be true. The compression ratio calculated at the same time should be 2 of N (n is a non-negative integer), because if not 2 of the N-square, will be four to the way to the nearest to the ratio of the nth number of the scale 2, the sample code is as follows:

1  Public Static intCalculateinsamplesize (2Bitmapfactory.options Options,intReqwidth,intreqheight) {3     //the picture is actually higher than the width4     Final intHeight =Options.outheight;5     Final intwidth =Options.outwidth;6     intInsamplesize = 1;7 8     if(Height > Reqheight | | width >reqwidth) {9 Ten         Final intHalfheight = HEIGHT/2; One         Final intHalfwidth = WIDTH/2; A  -         //calculates the maximum compression ratio, which is a power of 2 and guarantees that the actual picture width is greater than the width and height of the final display picture. -          while((halfheight/insamplesize) >Reqheight the&& (halfwidth/insamplesize) >reqwidth) { -Insamplesize *= 2; -         } -     } +     returninsamplesize; -}
Step three: Create a compressed picture

Do not forget that you need to set the injustdecodebounds of the object property before bitmapfactory.options to false, otherwise the resulting bitmap is null. The code is as follows:

1  Public StaticBitmap Decodesampledbitmapfromresource (Resources res,intResId,2         intReqwidth,intreqheight) {3 4     //First decode with injustdecodebounds=true to check dimensions5     FinalBitmapfactory.options Options =Newbitmapfactory.options ();6Options.injustdecodebounds =true;7 Bitmapfactory.decoderesource (res, resId, options);8 9     //Calculate insamplesizeTenOptions.insamplesize =calculateinsamplesize (Options, Reqwidth, reqheight); One  A     //Decode bitmap with Insamplesize set -Options.injustdecodebounds =false; -     returnBitmapfactory.decoderesource (res, resId, options); the}
Fourth step: Show compressed pictures

1 mimageview.setimagebitmap ( 2 decodesampledbitmapfromresource (Getresources (), R.id.myimage, 100, 100));

Android Load Large image

Related Article

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.