Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve drawable resource from Uri

Tags:

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.

like image 981
Daniele Vitali Avatar asked May 23 '13 15:05

Daniele Vitali


People also ask

How do I get Uri resources?

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.

How do I get the URI of a drawable image?

You should use ContentResolver to open resource URIs: Uri uri = Uri. parse("android. resource://your.package.here/drawable/image_name"); InputStream stream = getContentResolver().

How do I open a drawable file?

Inside Android Studio go to File -> Settings -> Plugins -> Browse repositories. Search Android Drawable Preview.


1 Answers

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); } 
like image 162
pskink Avatar answered Sep 27 '22 21:09

pskink