Daniele Vitali Daniele Vitali - 8 months ago 99
Android Question

Retrieve drawable resource from Uri

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.


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);