androidurldrawable

Get the URI of an image stored in drawable


I am adding a couple of sample items in my application so it doesn't look so empty when the user look at it the first time. The list with the sample items should have an image and the image I am going to use is already stored in the /res/drawable-folder of the application.

Since I already have a method that load the items images from a URI i'd like to get the URI to the /res/drawable/myImage.jpg but I don't seem to be able to get it right.

The flow is as follows: Create item with string that represents the URI of the image. Send list of items to an List The list loads the image in a background task by converting the string to URL and then run url.openStream();

I have tried a few options for the URI without any success. "android.resource://....." says unknow protocoll "file://" file not found

So right now I'm a little bit lost about how to fix this..


Solution

  • You should use ContentResolver to open resource URIs:

    Uri uri = Uri.parse("android.resource://your.package.here/drawable/image_name");
    InputStream stream = getContentResolver().openInputStream(uri);
    

    Also you can open file and content URIs using this method.