android – Retrieve drawable resource from Uri-ThrowExceptions

Exception or error:

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.

How to solve:

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

Leave a Reply

Your email address will not be published. Required fields are marked *