Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImageView.setImageURI does NOT work when trying to assign a R.drawable.X Uri

Tags:

android

is related to:

Need suggestion about a mixed "Uri / int id" images ambient

now my problem is that:

ImageView imgView=(ImageView)findViewById(R.id.imgView); Uri imgUri=Uri.parse("android.resource://my.package.name/"+R.drawable.image); imageView.setImageURI(imgUri); 

does NOT work . why?

i know that

imgView.setImageDrawable(Drawable.createFromStream(                     getContentResolver().openInputStream(imgUri),                     null)); 

work.

but that does NOT solve my problem. because I want to set the image with an uri independenty if this come from a resource or come from the camera ACTION_PICK intent...

any suggestions are welcome. Thank you. Regards

like image 958
Qlimax Avatar asked Feb 22 '10 18:02

Qlimax


People also ask

How do you make a drawable URI?

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

What is setImageUri?

ImageView. setImageUri only works for local Uri, ie a reference to a local disk file, not a URL to an image on the network. Here is an example of how to fetch a Bitmap from the network.


1 Answers

Try this

Uri imgUri=Uri.parse("android.resource://my.package.name/"+R.drawable.image); imageView.setImageURI(null);  imageView.setImageURI(imgUri); 

This is a workaround for refreshing an ImageButton, which tries to cache the previous image Uri. Passing null effectively resets it.

Solution suggested from this book: Sams Teach Yourself Android Application Development in 24 Hours - Highly recommendable to read.

like image 138
Norfeldt Avatar answered Oct 02 '22 17:10

Norfeldt