A bug in android2.2 source code about generating image thumbnails

Source: Internet
Author: User

Symptom: store a JPEG image with a special resolution on the sdcard, for example, 1X10000. Then, use the image library app that comes with Android to browse the image. You can use the image library to browse images in a thumbnail. In this case, the image library application exits abnormally. If you can view its log, you will find that memory overflow occurs when Dalvik allocates memory. However, if you use another image viewing tool to open the image without generating a thumbnail, no exception will occur.

Analysis: After analysis, it is found that there is an error in Google thumbnails. Google sets the resolution of all thumbnails to 96X96. Then, the scaling factor is calculated based on the resolution of the original image and the target image. The related functions are in the thumbnailutils. Java file. The path is:

/Framework/base/Media/Java/Android/Media/thumbnailutils. Java

The function is defined as follows:

/**

* Creates a centered bitmap of the desired size.

*

* @ Param source original bitmap source

* @ Param width targeted width

* @ Param height targeted height

* @ Param options used during thumbnail Extraction

*/

Public static bitmap extractthumbnail (

Bitmap source, int width, int height, int options ){

If (Source = NULL ){

Return NULL;

}

 

Float scale;

If (source. getwidth () <source. getheight ()){

Scale = width/(float) source. getwidth ();

} Else {

Scale = height/(float) source. getheight ();

}

 

Matrix matrix = new matrix ();

Matrix. setscale (scale, scale );

Bitmap thumbnail = transform (matrix, source, width, height,

Options_scale_up | options );

Return thumbnail;

}

If the resolution of the original image is 1600x1200, the calculated scale is:

Scale = 96/1200 = 0.08

The required memory size is 1600*0.08*1200*0.08*2 = 24 kbyte.

If the resolution of the original image is 1x10000, the calculated scale is:

Scale = 96/1 = 96

The required memory size is 1*96*10000*96*2 = 175 Mbyte,

The required space is 1*10000*2 = 19 kbyte.

Dalvik sets the memory size that each process can apply for. The default value is 16 Mb. The memory size of MB cannot be met by any system.

Conclusion: when processing scale, Google should only take into account the reduction of large images, but not the enlargement of small images, especially the enlargement of width, however, the high requirement must be reduced (or vice versa ). In order not to let the image library application exit unexpectedly, you can make some restrictions here. When the required memory is too large, you can not generate thumbnails.

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.