How to load image from assets in Jetpack Compose

2.7k Views Asked by At

I have hundreds of png files in assets folder and I want to load them into Image composable. But what I can use only images in drawable folder. how to load images from assets into Image?

2

There are 2 best solutions below

1
On BEST ANSWER

@Halifax solution works but I have some performance issue with it. by using Coil it is much smoother and cleaner.

in build.gradle (:app) :

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

and then:

AsyncImage(
            model = "file:///android_asset/images_folder/image.png",
            contentDescription = "",
        )
0
On

Example:

var bitmapState by remember{ mutableStateOf<Bitmap?>(null) }
val context = LocalContext.current

LaunchedEffect(Unit) {
    bitmapState = BitmapFactory.decodeStream(context.assets.open("assetsImage.png"))
}


if (null != bitmapState) {
    val bitmap = bitmapState!!.asImageBitmap()
    Image(
        bitmap = bitmap,
        "assetsImage",
        modifier = Modifier.fillMaxSize(),
        colorFilter = null
    )
}