How to get Drawable to Bitmap from AsyncImage Coil Jetpack Compose

369 views Asked by At

I am using an AsyncImage to load a image in my composable and there is function which generates PDF. In that function I want to pass the result of AsyncImage as a Bitmap. I can get the painter object in onSucess call of Async Image.

AsyncImage(
    model = imageUrl,
    contentDescription = null,
    modifier = Modifier
        .fillMaxWidth()
        .height(columnHeight.value),
    contentScale = ContentScale.FillBounds,
    fallback = fallbackImage,
    error = fallbackImage,
    placeholder = fallbackImage,
    onSuccess = {
        it.painter
    }
)

I tried to search for a option to convert painter into Bitmap in compose and was not able to do it. I was wondering if there is way to either convert the painter to bitmap or if there is option to get the Bitmap after async image has loaded.

1

There are 1 answers

0
Thracian On

You can get it from rememberAsyncImagePainter as

    val painter = rememberAsyncImagePainter(
        model = overlayImage,
    )

    var bitmap = remember<Bitmap?> {
        null
    }
 

    val imageState = painter.state
    if (imageState is AsyncImagePainter.State.Success) {
        bitmap = imageState.result.drawable.toBitmap()
    }

You can also refer this answer

https://stackoverflow.com/a/74686650/5457853