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..
You can use
Uri.Builder
instead of string concatenationThis is what you really need:
The easiest answer would be: Uri.parse(String goes here); //So, to make the drawable fit inside that bracket you just need to do this.
That's it.
based on above - tweaked version for any resource:
some info:
...
sources:
DISPUTE
this answer is correct, however the part about final fields is not - it has nothing to do with the answer – Boris Treukhov
@BorisTreukhov - please elaborate to us what u mean by "the part about final fields is not correct" - question - how to get uri to ...? construct the way it could be parsed (how is uri parsed ? see answer)
You should use
ContentResolver
to open resource URIs:Also you can open file and content URIs using this method.