Using Coil to Download Images on Android in Kotlin

A new way to download images on Android in Kotlin

Coil is a new image loading library in Kotlin for downloading images in Android, and it’s list of strengths are impressive:

  • Fast: Coil is fast because it performs a number of optimizations, including memory and disk caching
  • Lightweight: Coil adds only 1500 method counts to your APK file
  • Modern: Coil is built in Kotlin and uses modern libraries like Kotlin Coroutines, AndroidX LifeCycle, etc

Before Coil, we often used libraries like Glide and Picasso to download images in Android. But with the improvements that we’ll cover in this article, it’s clear that Coil is the go-to choice for Android devs working with Kotlin.

Requirements

To use Coil, you’ll need the following:

  • Android X
  • Min SDK 14+
  • Compile SDK: 29+
  • Java 8+

In this blog post, you’ll learn how to add Coil to your Android project and efficiently download images using it.

Using Coil on Android

First, you need to add a Coil library dependency to your Android application’s build.gradle file:

Now, just sync your project and you’ll be all set up. Next, let’s see how to actually download an image with Coil.

Download Image with an Extension Function

You can download the image using the extension function that Coil provides us: load() in the ImageView. Let’s check out the code for this:

Coil will download the image automatically and set the result in the ImageView.

Output:

Load Local Image Resource:

To load a local image, we just need to add an image in the extension function load(), as shown below:

Output:

Load Image File Path From Folder

To load a file path from your local storage, you just need to enter the path of your file, as shown in the below code:

Call the extension function load() and create a file object and pass your file path.

Add Transformations to the Image

In Coil, adding image transformations is relatively simple. Specifically, we have these transformations available:

  • CircleCropTransformation
  • RoundedCornersTransformation
  • BlurTransformation
  • GrayscaleTransformation

To test these out, let’s work with the CircleCropTransformation. Add this transformation using Coil is very simple, as shown in the below code snippet:

Output:

Proguard Rules:

By default, Coil is fully compatible with R8. If you’re using R8, then you won’t need to add any extra rules.

If you use Proguard, you may need to add rules for coroutines, OkHttp, and Okio.

If you want to explore the project, follow the below GitHub project link:

Conclusion

This article taught you to easily use Coil, Kotlin’s new library for downloading images in Android projects. Using the aforementioned extension functions, you can download images very easily, either from an external URL or from local storage.

I hope this article is helpful. If you think something is missing, have questions, or would like to give feedback, go ahead and leave a comment below. I’d appreciate the feedback.

I’ve written some other Android-related content, and if you liked what you read here, you’ll probably also enjoy this:

Sharing (knowledge) is caring 😊 Thanks for reading this article. Be sure to clap or recommend this article if you found it helpful. It means a lot to me.

If you need any help, join me on Twitter, LinkedIn, GitHub, and Subscribe to my Youtube Channel.

Avatar photo

Fritz

Our team has been at the forefront of Artificial Intelligence and Machine Learning research for more than 15 years and we're using our collective intelligence to help others learn, understand and grow using these new technologies in ethical and sustainable ways.

Comments 0 Responses

Leave a Reply

Your email address will not be published. Required fields are marked *