Skip to content Skip to sidebar Skip to footer

How Do I Load Url Into Image Into Drawimage In Compose Ui Android Jetpack?

I can use val image = +imageResource(R.drawable.header) and use DrawImage(image) to load image from Drawable Resource, But how do I load a string url into the DrawImage(image)?.

Solution 1:

Staring with 1.0.x the best way to achieve it is to use the Coil-Compose library.

Add in your build.gradle the dependency

dependencies {
    implementation("io.coil-kt:coil-compose:1.3.1")
}

Then just use:

Image(
    painter = rememberImagePainter("your url"),
    contentDescription = "My content description",
)

This loads the url passed in with rememberImagePainter, and then displays the resulting image using the standard Image composable.

Solution 2:

Coil for Jetpack Compose

Another option to load an image from the internet.

Add the Coil dependency to build.gradle :

dependencies {
    implementation "io.coil-kt:coil-compose:1.4.0")
}

Simple use:

Image(
    painter = rememberImagePainter("https://picsum.photos/300/300"),
    contentDescription = stringResource(R.string.image_content_desc)
)

Don't forget to add the internet permission (AndroidManifest.xml)

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

More custom here: Jetpack Compose - Coil Document

Solution 3:

A solution loading a Bitmap from the url and using the asImageAsset() Bitmap extension method :

@ComposablefunloadPicture(url: String): UiState<Bitmap> {
    var bitmapState: UiState<Bitmap> by state<UiState<Bitmap>> { UiState.Loading }

    Glide.with(ContextAmbient.current)
        .asBitmap()
        .load(url)
        .into(object : CustomTarget<Bitmap>() {
            overridefunonResourceReady(resource: Bitmap, transition: Transition<inBitmap>?) {
                bitmapState = UiState.Success(resource)
            }

            overridefunonLoadCleared(placeholder: Drawable?) { }
        })

    return bitmapState
}

Use the function with your Image() like this :

val loadPictureState = loadPicture(url)
if (loadPictureState is UiState.Success<Bitmap>)
    Image(loadPictureState.data.asImageAsset())
else
    Text("Loading...")

This snippet is using the Glide library and the UiState helper function from the JetNews official Google sample for Jetpack Compose

Solution 4:

You can use remember to watch a bitmap, then fetch it using Glide and update it as soon as you got it.

var bitmap by remember { mutableStateOf<Bitmap?>(null)}

Glide.with(ContextAmbient.current).asBitmap()
    .load("https://picsum.photos/200/300")
    .into(object : CustomTarget<Bitmap>() {
        overridefunonResourceReady(resource: Bitmap, transition: Transition<inBitmap>?) {
             bitmap = resource
          }

          overridefunonLoadCleared(placeholder: Drawable?) {}
      })

You can then use it like this:


if (bitmap != null )
    Image(bitmap!!.asImageAsset(), Modifier.fillMaxWidth())
else
    Text("Loading Image...")

(This is a modern version of bviale's answer)

Solution 5:

After Jetpack's beta release, using accompanist - Utils library for Jetpack Compose is the cleanest way for the above-mentioned use case.

Post a Comment for "How Do I Load Url Into Image Into Drawimage In Compose Ui Android Jetpack?"