3

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?

Hadi Ahmadi
  • 1,924
  • 2
  • 17
  • 38

2 Answers2

3

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
    )
}
Hadi Ahmadi
  • 1,924
  • 2
  • 17
  • 38
Halifax
  • 618
  • 3
  • 9
1

@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 = "",
        )
Hadi Ahmadi
  • 1,924
  • 2
  • 17
  • 38