下载同一张图片,在windows中可以看到图片文件的大小是102K的,而inputStream通过BitmapFactory.decodeStream()方法转成bitmap却要分配750k的内存。而我直接用FileOutputStream把inputStream写入文件,文件大小是102k和windows里的一样,请问这种情况怎么造成的?
巴扎黑2017-04-17 17:49:36
First of all, we need to clarify two concepts:
1. Bitmap: Here, what I understand is the bitmap image format in the Android system, which usually only exists in the memory and is used by the system to display images;
2. File: exists on the hard disk or Files on flash usually use some kind of compression method, such as jpeg or png.
Here, I use jpeg and bitmap as examples to compare and explain:
jpeg is compressed and suitable for storage, but cannot be displayed directly and needs to be decoded before it can be displayed;
bitmap is an uncompressed image, suitable for display, but not suitable for storage, because it takes up Too much storage space.
Let’s explain the phenomenon you see:
In Windows, you can see that the size of the image file is 102K: This is the compressed file;
Converting to bitmap through the BitmapFactory.decodeStream() method requires 750k of memory to be allocated : At this time, the compressed file is decompressed into a bitmap file in the memory. This is a format suitable for display. After decompression, the space occupied by the image becomes larger. The specific size has been clearly stated in okadanana's answer above;
Use FileOutputStream directly to write the inputStream to the file. The file size is 102k: This is to read the file into the memory without decoding and write the file as it is. Of course, the size does not change.
PHP中文网2017-04-17 17:49:36
Images are composed of pixels, so to calculate the size of an image, you need to know 3 parameters:
The length of the picture
The width of the picture
The memory size occupied by each pixel
Therefore, we get a formula
The memory size occupied by the picture = picture length x picture width x memory size occupied by each pixel
In Android, the memory size occupied by each pixel is determined by Bitmap.Config, which has 4 configurations
Configuration | Number of bytes occupied |
---|---|
Bitmap.Config.ALPHA_8 | One pixel, one byte |
Bitmap.Config.ARGB_4444 | One pixel 2 bytes |
Bitmap.Config.RGB_565 | One pixel 2 bytes |
Bitmap.Config.ARGB_8888 | One pixel is 4 bytes |
So, impact BitmapFactory.decodeStream()
生成的 Bitmap 的大小的是 Bitmap.Config
. So the size read out is naturally different.
伊谢尔伦2017-04-17 17:49:36
The image you download should be a jpg or png image. Both formats have a certain compression rate. After you decode it, it will exist in the form of rgb in the memory. Without compression, it will naturally be larger.