I am trying to load an asset in my Glance Composable with the AssetManager from Flutter. I found this documentation but it still uses Java and seems like Glance uses a different context than the application itself.
What I tried:
@Composable()
fun IconImage(modifier: GlanceModifier = GlanceModifier) {
val assetPath : String = "assets/test.png"
val assetLookupKey = FlutterLoader().getLookupKeyForAsset(assetPath)
val inputStream: InputStream = LocalContext.current.assets.open(assetLookupKey)
val bitmap = BitmapFactory.decodeStream(inputStream)
Image(
ImageProvider(bitmap), modifier = modifier, contentDescription = null
)
}
This worked:
@Composable()
fun IconImage(modifier: GlanceModifier = GlanceModifier) {
val assetPath : String = "assets/test.png"
val loader = FlutterInjector.instance().flutterLoader()
val assetLookupKey = loader.getLookupKeyForAsset(assetPath)
val inputStream: InputStream = LocalContext.current.assets.open(assetLookupKey)
val bitmap = BitmapFactory.decodeStream(inputStream)
Image(
ImageProvider(bitmap), modifier = modifier, contentDescription = null
)
}