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.
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.
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.
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
In Android, the memory size occupied by each pixel is determined by Bitmap.Config, which has 4 configurations
So, impact
BitmapFactory.decodeStream()
生成的 Bitmap 的大小的是Bitmap.Config
. So the size read out is naturally different.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.