androidflutterdartandroid-jetpack-composeglance

Load Flutter assets in Android Jetpack Glance


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
        )
}

Solution

  • 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
        )
    }