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.
Occasionally however, you will need to get the actual URI of a resource. All you need to do is replace the package name with your app's package name, as defined in the manifest, and the resource ID of the resource you would like to use. Uri resourceURI = Uri. parse("android.
You should use ContentResolver to open resource URIs: Uri uri = Uri. parse("android. resource://your.package.here/drawable/image_name"); InputStream stream = getContentResolver().
Inside Android Studio go to File -> Settings -> Plugins -> Browse repositories. Search Android Drawable Preview.
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); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With