Get the Uri of an Image Stored in Drawable

Get the URI of an image stored in drawable

You should use ContentResolver to open resource URIs:

Uri uri = Uri.parse("android.resource://your.package.here/drawable/image_name");
InputStream stream = getContentResolver().openInputStream(uri);

Also you can open file and content URIs using this method.

How to get path from image on drawable folder and set as image view, Bitmap?

You can save Reference Id R.drawable.img_name as integer rather than saving path.

When you need to manipulate this drawable you can use that id instead of R.drawable.img_name.

If you need a bitmap from this drawable follow this

Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
saved_id);

Retrieve drawable resource from Uri

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

How to save a selected Uri pic to drawable?

Check out this link Retrieve drawable resource from Uri

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


Related Topics



Leave a reply



Submit