How can I dynamically load resources in my mutlipl...
# multiplatform
c
How can I dynamically load resources in my mutliplatform project? I've got a number of images in the resources folder, but I won't know how many there are until runtime. I'd like to call
getResources()
to load them, but I don't know how to get a reference to the
Context
in order to do that.
h
Use this :
Copy code
compose.resources {
    publicResClass = true
    packageOfResClass = "sample.io.library.resources"
    generateResClass = always
}
add this in build.gradle then in class multiplatform can use like this: Res.drawable.sample_image_name
c
That doesn't work. I won't know ahead of time the names of the images or how many I'm loading.
I need a dynamic way to specify the resource to load.
h
For me it is work for dynamic way to call resource load
Copy code
class DrawableResource
@InternalResourceApi constructor(id: String, items: Set<ResourceItem>) : Resource(id, items)
c
Don't I need a
Context
to get a resource?
c
I've got a number of images in the resources folder, but I won't know how many there are until runtime.
You should clarify what you're doing, here. Usually, the resources folder is bundled into the binary (JAR…) at compile-time. So, there is no way to add resources between compile-time and run-time, unless the user unpackages the binary, updates the resources, and repackages them. What is the reason you can't know until run-time?
c
I have my screens defined in Json files. The Json files contain information on what images are needed for them. So the images are there, but aren't known to the code up front.
Obviously, I could hardcode all the resources I'm expecting into the program up front, but that means I'm constantly editing the code whenever the resources change.
And it's an unnecessary dependency.
c
So, everything is known at compile-time, but the code refers to the JSON files and not the images?
c
Right. Imagine a team of writers and artists editing text files and providing images. Some of the text files should include images when they're displayed, but not all of them. I don't want the writers editing code when they move things around or the artists editing code when they add or remove images.
c
You can create a mapping function that accepts a string and returns the resource.
c
It sounds more like I have to write a preprocessor which scans the directory, locates the images, writes their names into the autogenerated code, and have that write the mapping function.
Which is the whole reason I was trying to avoid this problem by locating them at runtime.
c
Why not just write the function by hand? Are the images a lot?
c
It's because the pipeline producing the text and the images is completely separate from the coding pipeline.
We'd rather not introduce dependencies between them.
Hardcoding links from the code to the resources means we need to manage and version them together.
c
I see...what if you used Res.getUri?
Since the JSON has the file names
c
Can I turn a URI into a DrawableResource?
I'm using
getBytes()
on the text side and that's working well.
c
BitmapDrawable can be created from a path or an input stream
c
It looks like the constructors for
BitmapDrawable
require a
Resources
object. The others are deprecated.
c
You can obtain Resources from the android context, but at this point, you'll have to do the iOS equivalent.
Use context.getResources()
c
That's where I started. How do I get the context?
c
It's in the MainActivity
applicationContext
c
Sure, on Android. What about JS and Desktop?
c
The android docs would help you here...I do think that this design isn't the best though.
You'd use the platform specific APIs
Do you have to bundle these resources?
If you had them downloaded remotely, this would be a lot easier
c
Yeah, maybe.
I'm just struggling to get anything working at all. My
Res.drawable.x
isn't seeing the
x.png
file I've got sitting in the resources directory.
And it insists
Res.drawable.foo
still exists, even though I deleted it an hour ago.
h
In order to to see x.png, you have to create folder unit test then run it. Then it will trigger to import that file. By the way in kotlin multiplatform we don't need context to call drawable image or file but instead must under @composable notation.
Every times, when you delete image from folder drawable you need to run unit test.
147 Views