DEV Community

Cover image for How does the Android Image Loading library solve the slow loading issue?
Amit Shekhar
Amit Shekhar

Posted on • Originally published at amitshekhar.me

How does the Android Image Loading library solve the slow loading issue?

I am Amit Shekhar, a mentor helping developers in getting high-paying tech jobs.

In this blog, we are going to learn how the Android Image Loading library solves the slow loading issue of images.

This article was originally published at amitshekhar.me.

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 Glide, Fresco, etc.

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 Glide and 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.

We use Glide like this:

Glide.with(fragment)
    .load(url)
    .into(imageView);
Enter fullscreen mode Exit fullscreen mode

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.

That's it for now.

Thanks

Amit Shekhar

You can connect with me on:

Top comments (0)