Hello, Does anyone knows how to get drawable image...
# android
a
Hello, Does anyone knows how to get drawable image path as a string inside my repository without using any context?
😶 4
a
You can't. Which resource file is selected for drawable resources is dependent on the context's configuration, and since multiple apps/apks can run in the same process, which apk a resource is loaded from is also dependent on the context.
👍🏻 1
i
Would this make sense for your use case?
Copy code
val resId = context.resources.getIdentifier(
    "your/image/path",
    "drawable",
    context.packageName
)
So you would have a plain string in the repository, but you can retrieve the actual identifier where you have access to the Context
a
I actually don't want to put context inside my repository. Then its kinda break of clean architecture and would do memory leak
I can also try to provide the image as a dependency by dagger not sure if its works.
i
No, I'm not saying you should put context inside the repo, but just convert your path (String) to a resourceIdentifier outside of the repo, probably in your view where you'll want to show the image
So the repo has the full control over which image path to send back to the view, you just use this little utility function where you have access to the context
Maybe I'm misunderstanding your use case - is the image bundled with the app, in the
res/drawable
? Or does the image come from web? I mean, either way, you should be able to work with String in the repo and load/convert/process/whatever you need to do with Context outside of the repo
a
I got your point thanks. So it also mean I can't even generate this image file path inside some non java/koltin file like inside json file?
Maybe I'm misunderstanding your use case - is the image bundled with the app, in the
res/drawable
?
yes. Its fallback image incase no network
i
So, is that really the responsibility of a repository?
Or does that belong to presentation layer?
a
I was doing the default data mapping inside repository when I see the issue with remote sources
i
Gotcha. I think it's totally fine to come back with
null
or nothing from the repository in which case presentation layer can show the placeholder image. Alternatively, you can actually start with the placeholder image and replace it with an actual image if/when you get it from the repo. Depending on your logic, but I don't think repo should be responsible for this. Like, you can load images in two ways: 1. Show placeholder right away and only replace it if you get an actual image from the repo 2. Show loading indicator while waiting and then replace it with an actual image or a placeholder image. In my opinion, in either case repo shouldn't know or care about how you want to present your image to the user
👍 1
a
Thanks for nice explanation. Appreciate it
i
Sure thing! Hope it helps. Cheers 👋
a
@Igor Milakovic you do not want to use
resources.getIdentifier
outside of debugging code paths. It's extremely slow.
i
Ha! Good to know, thanks
a
I think this will work
AppCompatResources.getDrawable(context, R.drawable.image)
902 Views