Android: access drawables outside an activity
-
05-10-2019 - |
Question
How do access a drawable which resides in my project's res/drawable folder from outside an activity? The component which handles the loading of images is generic and is not inside any activity. I would like to display an image from res/drawable in case it can't be fetched from the web.
Thanks, Rob
Solution
You need a reference to the Resources object. Easiest way would be to pass a reference to it in the constructor for your component.
OTHER TIPS
You can call it via your package.
For an image, the syntax would be: <your.package.name>.R.drawable.<filename>
Not quite as simple as that Zarah.
Just use PackageManager:
getDrawable (String packageName, int resid, ApplicationInfo appInfo)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow