https://kotlinlang.org logo
#compose
Title
# compose
g

Geert

11/24/2020, 10:51 AM
Are there no standard function to receive files from the resources folder? I have some json files included, but I can only find functions for imageassets, not for text.
j

Joost Klitsie

11/24/2020, 10:55 AM
What does a UI framework has to do with json files?
a

André Kindwall

11/24/2020, 10:56 AM
There is
stringResource
for strings.xml resources. If you want to get json assets then you can always get context with
ContextAmbient.current
j

Joost Klitsie

11/24/2020, 10:56 AM
I mean there are ways in Android to get files, and then there are ways to remember actions in compose. I'd say, you should combine it. I think if you check the code of the image asset loading you can probably make your own composable file loader
g

Geert

11/24/2020, 10:58 AM
@André Kindwall yeah I know. I was just wondering why there wasn’t a standard function
But what @Joost Klitsie said, it is a UI framework
I’m looking into it to much from an android perspective I guess
j

Joost Klitsie

11/24/2020, 11:03 AM
if you open
androidx.compose.ui.res.ImageResource.kt
I think you can copy/paste the code and adjust what you need. It looks like
loadResource
might help you out
j

Javier

11/24/2020, 1:06 PM
@Geert load a json from
resources
folder is not even an Android thing, you can achieve it with an extension function which only depends on JVM.
fun Any.readResource(file: String): String = this::class.java.classLoader.getResource(file)!!.readText()
👍 1
for example for reading the text of your json files
g

Geert

11/24/2020, 1:24 PM
Thank you! This is a nice option, never used classloaders before, except for testing.
j

Javier

11/24/2020, 1:45 PM
Yeah, I use it for testing too, that is the reason I use
!!