Search code examples
androidkotlinandroid-jetpack-composeandroid-jetpack-compose-material3

Display a `Drawable`


I'm working with JetPack Compose and I'm trying to display a Drawable (I want to display icons of other applications installed on the device):

val application: ResolveInfo;
val packageManager = LocalContext.current.packageManager;
val iconDrawable: Drawable = application.activityInfo.loadIcon(packageManager) // What to do with this?

I've been trying

Image(BitmapPainter(iconDrawable.toBitmap().asImageBitmap()), "")

but I get the error

java.lang.IllegalStateException: View needs to be laid out before calling drawToBitmap()

How can I display a Drawable ?


Solution

  • Add

    implementation "com.google.accompanist:accompanist-drawablepainter:<version>" 
    

    and use it like this

    @Composable fun DrawDrawable() {
        Image(
            painter = rememberDrawablePainter(drawable = drawable),
            contentDescription = "Content description"
        )
    }
    

    For more info refer :https://google.github.io/accompanist/drawablepainter/