Image compression in Android

Source: Internet
Author: User

1. The KB size of the heap memory used in Android computing is independent of the KB size of the image itself, and is calculated based on the size of the image.

For example, a 480*320 size picture takes up the heap memory size:

480*320*4/1024=600KB is multiplied by 4 because the ARGB image used in Android, a pixel occupies four bytes.

2, the cell phone factory heap memory is fixed, so in order not to cause oom, we need to generate bitmap when the image compression processing.

The actual use of our compressed image standard is the size of the mobile phone screen as a reference, this is mainly because, even if the picture size and screen size is the same, the conversion out of the memory size is much smaller than the mobile phone storage.

In order to explain the convenience, we take the 480*320 size of the mobile phone as an example, the picture compression for the same size as the phone screen, the size of 600kb, that is 0.6M, and 480*320 's mobile phone factory heap is generally 16M, the number according to each vendor difference.

3, compression mode: through the bitmap options

First of all, we need to know, when the picture is too large to load, the report oom where the anomaly is reported in?

The print test knows it is in: Bitmap Bitmap = bitmapfactory.decodefile (URL) This line is an error. So our handling is as follows:

1         //Get screen size2         intScreenWidth =Getwindowmanager (). Getdefaultdisplay (). getwidth ();3         intScreenHeight =Getwindowmanager (). Getdefaultdisplay (). GetHeight ();4          //How to get the screen size two: support for more than 13 version, is to use point storage width, and then take out. 5 //Point point=new Point ();6 //Getwindowmanager (). Getdefaultdisplay (). GetSize (point);7 //Screenwidth=point.x;8 //Screenheight=point.y;9               TenBitmapfactory.options opts=Newbitmapfactory.options (); OneOpts.injustdecodebounds =true;//set to True for the purpose of getting a picture of some of the attached information without actually loading the picture A Bitmapfactory.decodefile (photopath,opts); -         //get the dimensions of a picture -         intImgwidth=Opts.outwidth; the         intimgheight=Opts.outheight; -         //Select a zoom method based on the comparison size -opts.insamplesize=imgwidth/screenwidth>imgheight/screenheight?imgwidth/screenwidth:imgheight/ScreenHeight; -Opts.injustdecodebounds =false;//allow pictures to be loaded +Bitmap mimage = Bitmapfactory.decodefile (Photopath, opts);//load a picture in compressed proportions -Bytearrayoutputstream BAOs =NewBytearrayoutputstream (); +  AImage.setimagebitmap (Mimage);

Summarize:

# #缩放加载大图片
1. Get the size of the picture you want to display, and if it is full screen, get the screen size

2. Get the original size of the picture, do not actually load the picture into memory, only get the information of the picture
Options opts = new options ();
Set to True, do not add in memory, just take information
Opts.injustdecodebounds = true;
3. Compress the image, the premise is that the size of the picture is larger than the size of the area you want to display.
Calculate according to the ratio of width or height, select the one with large scale to compress
The compressed scale value is finally copied to
Opts.insamplesize = scale;
4. When the proportions are set, it's time to load the image into memory.
Opts.injustdecodebounds = false;
5. The last part is the loading picture
Bitmap Bitmap = Bitmapfactory.decodefile ("/mnt/sdcard/dog.jpg", opts);

The ultimate goal of doing so much is to opts.insamplesize = scale.

Image compression in Android

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.