How to load and display an image from a URL using Jetpack Compose and Kotlin

Oscar de la Hera Gomez
Four flowers that represent Kotlin, Android, Jetpack Compose and Android Studio side by side. Beneath them sits the text “Load Image from URL.”

A step by step guide on loading an image from a URL and displaying it in an Android app using Jetpack Compose and Kotlin.

The following tutorial builds on our Open Source Kotlin project which can be downloaded using the link below, and aims to walk you through how to setup an Android project to load images from a URL and place them in an Android app using Jetpack Compose.

We recommend that you checkout the main branch and carry out the steps described below. The changes that took place can be found on the tutorial/data/image-from-url branch.

git clone git@github.com:delasign/kotlin-android-starter-project.git

Step One: Add Internet Permissions

A screenshot of Android Studio showing the AndroidManifest.xml file. Highlighted is where we have added the permissions - this is above the application section of the manifest.

In the projects AndroidManifest.xml file, add the following permission:

 <uses-permission android:name="android.permission.INTERNET" />

Step Two: Add the Dependency

A screenshot of Android Studio showing the AndroidManifest.xml file. Highlighted is the dependency whose code is provided below.

In the App Level build.gradle.kts file, add the following dependency:

implementation("io.coil-kt:coil-compose:2.4.0")

Please note that versions displayed above may not be the latest versions, please update the code above to the latest version in your Android Studio project.

Step Three: Sync the Gradles

A screenshot of Android Studio showing a gradle file that has recently been modified. Highlighted is the "Sync Now" button that appears on the right of the prompt at the top of the Gradle file. Click this button to sync the gradle files with the project.

Press Sync Now in the bar above the Gradle to update the project dependencies.

Step Four: Load and Display the Image

A screenshot of Android Studio showing the code required to load an image from a URL. This is provided below.

Load and display the image in a composable, using code similar to that below:

Please note that the placeholder drawable is what's shown when the image is loading and the error drawable is what gets drawn if the image does not load.

If you wish to create a loading indicator use the onLoading, onSuccess and onError listeners along with mutable states, modifiers and more complex Composable visual logic to show the indicator whilst the image is loading.

A screenshot of Android Studio showing you the documentation that's available on highlight for the AsyncImage.

Please note that it is suggest that you localize your images to ensure that they can be read by people with dissabilities.

Step Five: Test

A screenshot of Android Studio showing the image successfully loaded in the simulator.

Run the app to check that the image loads correctly.

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
delasign logo

Book a Free Consultation.

An icon of an email.

Click here to email us.

Fill in the details below to book a free consultation or to let us know about something else. Whatever it is, we are here to help.

How can we help you ?

Contact Details