Loading Large Bitmaps Efficiently in Android
Loading large bitmaps into memory is always a pain. We all see OOM(Out Of Memory) errors in our crash reports because of large images. Android has a limited memory as we all know. We have to keep this in mind
There are lots of stackoverflow questions about that and you can just skip this blogpost and keep copy-pasting when you have OOM :) But for the rest, I want to give some information about loading large bitmap and how it actually works.
I wanted to give you logic behind decoding bitmaps. I suggest you to use Picasso or Glide to load image. No need to reinvent the wheel.
Load Bitmap Into Memory
It is easy. All you need is decode your image using BitmapFactory.
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.hqimage);
imageView.setImageBitmap(bitmap);
Seems everything is OK. But there is a problem which i am going to tell you. Let’s check out decoded bitmap size in our memory.
bitmap.getByteCount() method will return it’s size. Here is the total bytes of bitmap in the memory: 12262248 Bytes, which equals to 12,3 MB. Yes, you might be confused. You may think that Image’s actual size on disk is about 3.5 MB and getByteCount() is showing larger than disk size. Here is the reason:
The image is compressed when it is on disk (stored in a JPG, PNG, or similar format). Once you load the image into memory, it is no longer compressed and takes up as much memory as is necessary for all the pixels.
Steps
- Get size of image without loading into memory
- Calculate scale factor with image’s size.
- Load bitmap into memory with calculated values.
BitmapFactory.Options
This class is a metadata provider for us. We can use this class to achieve first step.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.mipmap.hqimage, options);
We pass BitmapFactory.Options instance to BitmapFactory.decodeSource() method. You can see that we configured our “options” by setting inJustDecodeBounds true. What is the meaning of inJustDecodeBounds? It means that we don’t want to load bitmap into memory. We just want to get information(width, height, etc.) about image. So we can calculate scale factor with that information.
When we run this code and log options value:
options.outHeight : 1126
options.outWidth : 2000
options.bitmap : null
Here is the result. We have height, width. And I just wanted to see if bitmap is really null. Crosscheck: done.
Reducing Image Size (In Memory)
Now it is time to calculate inSampleSize. Wait. What is inSampleSize? inSampleSize is a scale factor that belongs to BitmapFactory.Options class.
If we have an image 1000x1000 and we set inSampleSize 2 before decoding. We will have 500x500 image after decoding operation. If we have 200x400 image and we set inSampleSize 5, we will have 40x80 image after decoding.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inSampleSize = 3;
BitmapFactory.decodeResource(getResources(), R.mipmap.hqimage, options);
Can we use it just like this? No. Because we don’t know what image size is. If it is small image and we make it more smaller, our user can see some pixels instead of image. Some images have to be scaled down 5 times. Some images have to be scaled down 2 times. We can not set scale factor as a constant. So we have to do a calculation according to image size.
Calculating inSampleSize is up to you. I mean, you can write your algorithm according to your needs. In android documentation, they calculate it power of two based. But you can also calculate your inSampleSize by incrementin it 1 by 1.
You can check the inSampleSize calculation code from android documentation.
options.inSampleSize = calculateInSampleSize(options, 500,500);
options.inJustDecodeBounds = false;
Bitmap smallBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.hqimage, options);
We switch inJustDecodeBounds to false and get bitmap with options instance.
Now, bitmap.getByteCount() method will return 3.1 MB. This is in memory size. As I said before, images are compressed when it is on disk. They are larger when we load them into memory.
We reduced from 12.3 MB to 3.1 MB. It is reduced %75 in MEMORY.
Reducing Image Size (In Disk)
We can also reduce image size in disk. We can compress our bitmap by using compress method of Bitmap.
Let’s check the file size without changing quality of bitmap image. 100 means same quality.
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
byte[] bitmapdata = bos.toByteArray();
When I calculate for original image, result is 1.6 MB on disk. Let’s change the quality and check the file size again.
bitmap.compress(Bitmap.CompressFormat.JPEG, 50, bos);
I changed the quality to 50. And result is 24.4 KB.
Compress Format should be .JPEG if we want to change quality of bitmap. Quality can not be changed in PNG format.
We reduced file size from 1.6 MB to 24.4 KB.
Here is the result.
Don’t hesitate to ask/add something. Comments are welcome.
Happy coding.