A Bitmap story

Clint Paul
4 min readMar 19, 2022
Photo by Nong V on Unsplash

The article was originally posted at clintpauldev.com

Do you think that bitmaps can be dangerous.? Can it take an immense amount of memory of your device every time you deal with it.? To get clarity about it, take a picture from your android device in its full resolution and check the size of the image.

I took it from a POCO M2 Pro. It takes up to 5 MB for a single picture. What if we take it from a high-end camera device like a Pixel.? In its full resolution(4048x3036 or 12 megapixels), the size of a picture can go up to 48MB. Feel the heat now? Yes. Bitmaps can certainly exhaust your app’s memory budget. In this blog, I will try to explain what are bitmaps and how we can handle them properly.

What is a Bitmap?

It is a box of dots. When viewed at 100%, each of these dots corresponds to pixels on display. Each of these pixels can be a different color as well. Check the above bitmap of a lady. Pixels gave life to her blonde hair, green jacket, fair face, etc.

Let’s see how we can create a simple Bitmap,

Bitmap b = Bitmap.createBitmap(250, 250, Bitmap.Config.ARGB_8888)

The first two parameters correspond to width and height in pixels. The third parameter defines the type of pixel. This parameter helps you to decide the quality of the picture you need, the ability to display transparent/translucent colors, etc. In most cases, we will use Bitmap.Config.ARGB_8888 because it gives us the best quality and flexibility. If you want to know in detail about Bitmap configurations, please check this link.

Why loading bitmaps is tricky

  • We have already established that Bitmaps can take a massive amount of your app’s allotted memory if not dealt with care.
  • Loading bitmap on the UI thread is another big mistake. Handle it in a background thread to reduce slowness and tackle unwanted ANRs.
  • Android recommends we use a stable image loading library like Glide, Coil, or Picasso. Since these libraries already have a superb mechanism to deal with the above concerns and are painless to implement it in your app.

But, what if you want to handle bitmaps without the help of any third-party libraries.? Is it possible.?

How to handle bitmaps gracefully

Consider a scenario where you have to load a picture taken by the user, using his device’s camera, as his profile picture. We can call it a thumbnail because it will be a smaller version of its actual size and shape. So, do you think it is worth loading a 5 MB image to a circle image or square image view.? A higher resolution image doesn’t provide any considerable advantage in this use case. It is better to scale it down. Let’s see how we can do it.

We can use the createScaledBitmap method to resize any bitmap to our desired width and height.

We can pass our desired width and height. Also, we have to make sure it maintains an aspect ratio. Else, the scaled-down image will look weird on display.

Another thing to note is that don’t ever keep bitmaps in a list or something. As you saw earlier, a single image could take up to 5 MBs. What if we are dealing with ten images or something, and we unknowingly stored it in a list.? It can easily take up to 50 MBs of your app’s allotted memory, and now you will have to run the remaining operations with limited storage.

Also, if you are loading bitmaps after one another, do recycle before the newer one starts decoding. Why.? Because the first bitmap is not garbage collected yet. The garbage collector will decide later. So, it is better to manually do it if you want to free up the memory ASAP.

You can use Bitmap.recycle() for this.

I hope you have enjoyed reading this article. If you have any questions or want to add any missing points, please comment. Thank you for your time.

You may also want to read my recent articles on,

--

--

Clint Paul

Software Engineer @ShareChat. I love to read and write.