Loading large images into memory can be a headache. Because of the big picture, we will see OOM in the Crash report.Android has limited memory, which we should be aware of.

1.1 Loading Bitmap to memory

The next step is to use the BitmapFactory to decode your image.

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.hqimage);
imageView.setImageBitmap(bitmap);
Copy the code

Does it look like we’re done? I’m going to tell you there’s a problem. Let’s take a look at the memory footprint of the decoded image. Bitmap.getbytecount () returns the size of the image. Its memory size is 12262248 bytes, which is equal to 12.3 MEgabytes. Yes, you might be wondering. The size of the image on disk is 3.5MB, but getByteCount() results in more than 3.5MB. Here’s why: Images are compressed in JPG,PNG or other formats and stored on a computer. Once you load an image into memory, the image will not be compressed, but will take up all the memory required for the pixels.

Step 1.2

  • Get the size without loading the image into memory
  • Calculate the scaling factor from the image size
  • Load the image into memory by calculating the post-value

1.3 BitmapFactory. The Options

This class is a metadata provider, and we can use this class to do the first step.

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.mipmap.hqimage, options);
Copy the code

We preach BitmapFactory. Examples of the Options to BitmapFactory. DecodeSource () method. We set inJustDecodeBounds to True. What does inJustDecodeBounds mean? That is, we don’t have to talk about loading images into memory. We just want to get information about the picture. We can use this information to calculate the scaling factor. Running this code yields the following information:

options.outHeight : 1126
options.outWidth : 2000
options.bitmap : null
Copy the code

1.4 Reducing image size (in memory)

Now it’s time to compute inSampleSize, wait, what’s inSampleSize? InSampleSize is a scale factor belonging to the bitmapFactory. Option class. If we have a 1,000 card

1000 image, inSampleSize 2, decoded size 500

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inSampleSize = 3; 
BitmapFactory.decodeResource(getResources(), R.mipmap.hqimage, options);
Copy the code

Can we play like this? I can’t. Since we do not know the size of the image, if it is small, the image will become smaller and our user will only see some pixels instead of the image. Some images will be scaled by 5 times, others by 2 times. We define the scaling factor as a constant. So we need to calculate according to the picture size. The method of calculating inSampleSize is up to you, by which I mean design your own algorithm according to your needs. InSampleSize is calculated as a power of 2 in the Android documentation.

Options. InSampleSize = calculateInSampleSize (options, 500500); options.inJustDecodeBounds =false;
Bitmap smallBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.hqimage, options);
Copy the code

We changed inJustDecodeBounds to false to get the Bitmap. Bitmap.getbytecount () now returns a size of 3.1MB. This is the size of memory. Image memory size changed from 12.3MB to 3.1MB, reducing memory size by 75%.

1.5 Reducing image Size (on disk)

We can also reduce the size of the image on disk. We can compress images using Bitmap’s compress method. Let’s look at the size of the image without changing the edge. 100 means the quality is unchanged

ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
byte[] bitmapdata = bos.toByteArray();
Copy the code

The original image size is 1.6MB, how will the file size change if the image quality is changed?

bitmap.compress(Bitmap.CompressFormat.JPEG, 50, bos);
Copy the code

Image quality changed to 50 and size changed to 24.4KB if you want to change the quality of the bitmap, the compression format must be. JPEG, PNG image quality does not change. File size changed from 1.6MB to 24.4KB.

eggs

Corresponding learning video: learn to use Android advanced skills large long diagram loading principle and handwritten implementation

Here I also share an Android learning PDF+ architecture video + interview documents + core notes compiled by several leaders, advanced architecture technology advanced brain map, Android development interview topic information, advanced architecture information

These are the best materials that I read again and again in my spare time. Can effectively help you master the knowledge, understand the principle. Of course, you can also take to check and fill gaps, promote their competitiveness.

You can pick it up here if you need it

If you like this post, please give me a thumbs-up, leave a comment or share your support