- Amit Shekhar
- Published on
I am Amit Shekhar, a mentor helping developers in getting high-paying tech jobs.
Before we start, I would like to mention that, I have released a video playlist to help you crack the Android Interview: Check out Android Interview Questions and Answers.
In this blog, we are going to learn how the Android Image Loading library solves the slow loading issue of images.
This blog is a part of the series - "How does the Android Image Loading library work internally?"
- How does the Android Image Loading library use the bitmap pool for responsive UI? - READ FROM HERE
- How does the Android Image Loading library solve the slow loading issue? - YOU ARE HERE
- How does the Android Image Loading library optimize memory usage? - READ FROM HERE
Loading images fast is very important for a better user experience. We must learn how it is done under the hood.
These concepts are important when it comes to Android Interviews.
So, I was reading the source code of various Image Loading libraries in Android like
These libraries do a lot of things internally for us so that we can just use them and get our tasks done easily. They make our life easier.
One of the things that they do is that they load the images as quickly as possible.
Let's learn how the Android Image Loading library loads the image fast.
Slow loading is another problem when it comes to loading a bitmap into the view. One of the major reasons for slow loading is that we do not cancel the task like downloading, or decoding a bitmap even when the view is out of the window or that task is no more needed, hence there are many tasks that are being done even though we do not need them, so it takes time for the actual image to load which just came in the window.
The image loading libraries like
Fresco take care of this, they cancel all the tasks properly and only load the images which are visible to the user.
These libraries are aware of the activity, and fragment lifecycle, this way they know which image downloading or bitmap decoding tasks need to be canceled.
You must be thinking about how they are aware of the activity and fragment lifecycle. I will explain this to you by taking the example of
Glide like this:
Glide.with(fragment) .load(url) .into(imageView);
Here, we can see that we are passing the fragment, this way
Glide subscribes to the Fragment's lifecycle events. Similarly, it happens for the activity and the view.
This is how they solve the slow image loading problem by canceling the tasks that are no more needed at the appropriate time. One more thing that these libraries do is that they cache the images at two levels. Let's learn about it also.
They create a memory cache so that they do not have to decode the image again and again as decoding takes time. These libraries create a cache of some configurable size to catch the bitmaps.
They maintain two levels of caching:
- Memory Cache
- Disk Cache
When we provide the URL to the libraries, they do the following:
- They check if the image with that URL key is available in the memory cache or not.
- If present in the memory cache, they just show the bitmap by taking it from the memory cache.
- If not present in the memory cache, they check in the disk cache.
- If present in the disk cache, they load the bitmap from the disk, also puts it in the memory cache, and load the bitmap into the view.
- If not present in the disk cache, they download the image from the network, put it in the disk cache, also put it in the memory cache, and load the bitmap into the view.
This way they make loading fast as showing directly from the memory cache is always faster.
So mainly, they do these two things as follows:
- Cancel the tasks that are no more needed at the appropriate time.
- Cache the images at two levels.
This was all about how the Android Image Loading library solves the slow loading issue of images.
We will learn more things that these libraries do in our upcoming blog post.
Master Kotlin Coroutines from here: Mastering Kotlin Coroutines
That's it for now.
You can connect with me on: