views:

346

answers:

2

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

A: 

You need a reference to the Resources object. Easiest way would be to pass a reference to it in the constructor for your component.

JRL
Thanks for your reply.I also thought about what you suggest but my image loader is a singleton. Besides, I would like to keep the image loader as generic as possible. I tought of using Drawable.createFromPath(String path) but can't figure out the correct path...
Rob
@Rob: drawables that are in res/drawable are compiled into your apk and are thus not accessible as files. If you want them to be, you'll have to create files on the phone for them, which you'll then be able to access using a path.
JRL
Thanks for the clarification.
Rob
@Rob: as an ugly hack, you can set a static Resource reference from within the onCreate method of your launcher activity.
JRL
A: 

You can call it via your package.

For an image, the syntax would be: <your.package.name>.R.drawable.<filename>

Zarah