Retrieve drawable resource from Uri

Daniele Vitali picture Daniele Vitali · May 23, 2013 · Viewed 26.7k times · Source

I'm storing the drawable resource path in an Uri in this way:

Uri uri = Uri.parse("android.resource://my_app_package/drawable/drawable_name");

How do I get the Drawable that the uri is referencing?

I don't want to put it in an ImageView, just retrieve the Drawable (or Bitmap) object.

Answer

pskink picture pskink · May 23, 2013

getContentResolver cr object then call:

is = cr.openInputStream(uri) 

and finally call:

Drawable.createFromStream(InputStream is, String srcName)

...Here's an actual working code example so you can see for yourself:

try {
    InputStream inputStream = getContentResolver().openInputStream(yourUri);
    yourDrawable = Drawable.createFromStream(inputStream, yourUri.toString() );
} catch (FileNotFoundException e) {
    yourDrawable = getResources().getDrawable(R.drawable.default_image);
}