Bitmap image compression size for Android programming

Source: Internet
Author: User

The Bitmap image is too large, causing memory overflow. Generally, the image preview mechanism is used to obtain an image with a smaller size.

Here, the so-called scale-out does not mean to set the width and height in layout to reduce the image size (in essence, it is still a large memory image), but to actually reduce the image size and reduce memory usage.

The methods are described in the annotations below:
[Java]
Private Bitmap revitionImageSize (String path, int size) throws IOException {
// Obtain the image
InputStream temp = this. getAssets (). open (path );
BitmapFactory. Options options = new BitmapFactory. Options ();
// This parameter indicates that, without allocating memory space for bitmap, only the information of the image (such as the size of the image) is recorded. To put it bluntly, the memory is optimized.
Options. inJustDecodeBounds = true;
// Obtain the options content by creating an image (here the value is assigned by passing the java address)
BitmapFactory. decodeStream (temp, null, options );
// Close the stream
Temp. close ();
 
// Generate a compressed image
Int I = 0;
Bitmap bitmap = null;
While (true ){
// This step satisfies the width and height according to the size to be set.
If (options. outWidth> I <= size)
& (Options. outHeight> I <= size )){
// Re-obtain the stream. Note: The stream must be loaded again. You cannot use the previous stream again!
Temp = this. getAssets (). open (path );
// This parameter indicates that the newly generated image is a fraction of the original image.
Options. inSampleSize = (int) Math. pow (2.0D, I );
// If this parameter is set to true, set it to false. Otherwise, no image is created.
Options. inJustDecodeBounds = false; www.2cto.com
 
Bitmap = BitmapFactory. decodeStream (temp, null, options );
Break;
}
I + = 1;
}
Return bitmap;


:

Author: xyz_fly

 


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.