Instance method of Android loading large resolution pictures into mobile phone memory

Restore the heap memory overflow error. First, restore the heap memory overflow error. First, put a photo on the SD card with a resolution of (3776 x 2520) and a size of 3.88MB. It is a photo taken by my own camera. The layout of the application is very simple, one button and one ImageView. Then, in the conventional way, use bitmapfactory to load a photo and use an ImageView to display it. The code is as follows:

After clicking the button, the program will report an error, and the view log is:

Let's first analyze this error. First, dalvikvm (Android virtual machine) finds that the required memory is 38mb, which is larger than the application heap memory of 24MB. At this time, it tries to load data by soft loading. We know that dalvikvm will automatically perform GC (garbage collection) when the memory is insufficient. It cleans up about 55k of space, which takes 203 milliseconds, but the memory is still insufficient, Therefore, the heap memory overflow error finally occurs. Analyze heap memory overflow. Android system is mainly used for mobile devices with low energy consumption, so there are many restrictions on memory management. For an application, Android system will allocate a maximum of 16MB (24MB for some models) as heap memory space by default. The simulator I use here is debugged, and this simulator is set to 24MB, It can be viewed in Android virtual device manager.

The picture here clearly only has 3.88MB, which is far less than the heap memory allocated by Android for applications. Why does it need to consume about 38mb of memory when loaded into memory? As we all know, a picture is composed of a point distribution (resolution). Usually, loading such data will create a two-dimensional array in memory. Each item in the array represents a point, and the resolution of the picture is 3776 * 2520. Each point is composed of ARGB colors, and each pigment accounts for 4 bytes, Therefore, the memory required to load this picture into memory is: 3776 * 2520 * 4byte = 38062080byte. It takes about 38mb of memory to correctly load this picture. This is the error description above. 38mb of memory space is required, and the size is slightly different, because there are some EXIF information to be stored in the picture, which will be larger than that calculated only by resolution. How to load high-resolution pictures sometimes we do need to load some high-resolution pictures, but for mobile devices, even if the loading can succeed, so much memory is a waste (screen resolution limit), so we need to find a way to compress the pictures according to a certain ratio to reduce the resolution, so that we don't need to consume a lot of heap memory space, The resolution of the device screen can be used to display pictures. Here we use a bitmapfactory.options object, which is described below. Bitmapfactory.options is an internal class of bitmapfactory. It is mainly used to set and store some information of bitmapfactory loaded pictures. The following attributes are needed in options: injustdecodebounds: if set to true, the pixel array of the picture will not be loaded into memory, but only some additional data will be loaded into options. Outheight: the height of the picture. Outwidth: the width of the picture. Insamplesize: if set, the picture will be loaded according to this sampling rate and cannot be set to a number less than 1. For example, if it is set to 4, the resolution width and height will be 1 / 4 of the original. At this time, the overall memory will be 1 / 16 of the original.

The following is a simple demo to demonstrate the contents mentioned above. The comments in the code are relatively clear, so I won't repeat them here.

Effect display:

Summary here explains how to load a large resolution picture into memory and use it. However, generally better image processing software will have the image amplification function. If only this processing is done, simply enlarging the processed image will affect the display effect and the image restoration degree is not high. Generally, the resolution pixel array of the picture in the enlarged area will be retrieved, and then reprocessed and loaded into memory for display.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>