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.