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
?
Asked
Active
Viewed 1,247 times
3

Halit Arslan
- 13
- 4

Hadi Ahmadi
- 1,924
- 2
- 17
- 38
2 Answers
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
-
I tried to show a svg file but it did not appear - any help? – VFarkas2023 Jun 21 '23 at 14:47