简体   繁体   English

Jetpack Compose 在变量更改时如何处理图像?

[英]Jetpack Compose how do i work with images when variable changes?

I use the composable function image.我使用可组合的 function 图像。 I need to use the specific image depending on the variable it gets.我需要根据它获得的变量来使用特定的图像。 it looks like the code underneath when hardcoded and this works for the specific image.硬编码时它看起来像下面的代码,这适用于特定图像。

Image(
       painterResource(id = R.drawable.carrot),
       contentDescription = "carrot",
       )

The problem is i want it to work like the picture below where recipe.image changes depending on what it gets from the database.问题是我希望它像下面的图片一样工作,其中 recipe.image 的变化取决于它从数据库中获取的内容。

Image(
       painterResource(id = recipe.image),
       contentDescription = "recipe.name",    
            )

Somehow it needs the exact path to the drawable that is saved localy.不知何故,它需要本地保存的可绘制对象的确切路径。

What i have tried: I've tried to upload R.drawable.carrot directly to the database as an Integer, it saves it as something like 2400440340, when this integer gets into the painterResource it crashes.我已经尝试过:我尝试将 R.drawable.carrot 作为 Integer 直接上传到数据库,它将它保存为类似 2400440340 的东西,当这个 Z157DB7DF5366CResource6 崩溃时它会崩溃I've tried to save the "R.drawable.carrot" directly as a string into the database but that don't work either because the painter needs an integer.我尝试将“R.drawable.carrot”直接作为字符串保存到数据库中,但这也不起作用,因为画家需要 integer。

The only suggestions i can find to this problem on this site is this answer from 11 years ago:我能在这个网站上找到这个问题的唯一建议是 11 年前的这个答案:

int drawableId = getResources().getIdentifier(drawablename, "drawable", getPackageName());
imageview.setImageResource(drawableId);

I can't seem to find the functions getResources or the getIdentifier.我似乎找不到函数 getResources 或 getIdentifier。

What i have tried: I've tried to upload R.drawable.carrot directly to the database as an Integer, it saves it as something like 2400440340, when this integer gets into the painterResource it crashes.我已经尝试过:我尝试将 R.drawable.carrot 作为 Integer 直接上传到数据库,它将它保存为类似 2400440340 的东西,当这个 Z157DB7DF5366CResource6 崩溃时它会崩溃

It crashes because this Int value in Resources is just a runtime reference, which means if you restart the app, the Resource, for example R.drawable.carrot , gets new referenced.它崩溃是因为 Resources 中的这个Int值只是一个运行时引用,这意味着如果您重新启动应用程序,资源,例如R.drawable.carrot ,将获得新的引用。 If you want another image in your painterResource , you have to change the value of recipe.name somewhere or you save your image binaries in the database (which is not very performant, I recommend not to do that).如果您想要在您的painterResource中添加另一个图像,您必须在某处更改recipe.name的值,或者将您的图像二进制文件保存在数据库中(这不是很高效,我建议不要这样做)。

You can load them over assets: If you dont't have an assets folder, creat one in this location: app/src/main/assets/您可以通过资产加载它们:如果您没有资产文件夹,请在此位置创建一个: app/src/main/assets/

And then you can access it with a function like this:然后您可以使用 function 访问它,如下所示:

    fun getImageFromAssets(imageName: String): Bitmap {
        val imageStream = assets.open(imageName)
        val bitmap = BitmapFactory.decodeStream(imageStream)
        imageStream.close()
        return bitmap
    }

Android Studio will suggest you to "Inline" the variable but don't do that. Android Studio 会建议您“内联”变量,但不要这样做。 If you Inline the BitmapFactory at the return point, you close the Stream before you decode it.如果在返回点内联BitmapFactory ,则在解码之前关闭Stream

You can now simply call the name from the database like:您现在可以简单地从数据库中调用名称,例如:

val image: Bitmap = getImageFromAssets("imageName.jpg")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 布局在 Jetpack Compose 中如何工作以及它们与 XML 有何关系? - How do layouts work in Jetpack Compose and how do they relate to XML? 如何在 Jetpack Compose 中关闭主线程? - How to do work off the main thread in Jetpack Compose? Jetpack Compose:列表更改时更新可组合 - Jetpack Compose: Update composable when list changes 自定义文本字段 jetpack 撰写,我不知道如何处理文本字段 - custom textfield jetpack compose, I do not know how to do with Textfield Jetpack compose - 当应用程序返回前台时如何刷新屏幕 - Jetpack compose - how do I refresh a screen when app returns to foreground 如何直接在 Jetpack Compose 中使用 Color 资源? - How do I use Color resource directly in Jetpack Compose? 在 Jetpack Compose 中,如何应用存储在 Assets 中的字体/字体? - In Jetpack Compose, how do i apply a font/typeface stored in Assets? 如何在 Jetpack Compose 中为文本添加点/虚线下划线? - How do I make a dotted/dashed underline for Text in Jetpack Compose? 如何在 Android Jetpack Compose 中禁用可组合预览? - How do I disable the composable preview in Android Jetpack Compose? 如何在 Jetpack Compose 中测试带注释的字符串超链接点击? - How do I test an annotated string hyperlink click in Jetpack Compose?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM