32

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

Community
  • 1
  • 1
Qlimax
  • 5,101
  • 4
  • 27
  • 30
  • 1
    I can't belive that nobody had the same problem...nor there is no solutions... why stuff like that are not working?why are not documented at all? – Qlimax Feb 24 '10 at 16:04

4 Answers4

77

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.

Tim
  • 39,651
  • 17
  • 123
  • 137
Norfeldt
  • 6,180
  • 16
  • 85
  • 131
0

imageView.postInvalidate() works. Or imageView.invalidate() if you're on the UI thread.

Christine
  • 5,479
  • 4
  • 38
  • 61
0

your should do something like this

Uri imgUri = Uri.parse("android.resource://my.package.name/drawable/image_sky");

or if you want like this

Uri imgUri = Uri.parse("android.resource://" + getPackageName() + "/drawable/" + "image_sky");
Dan Alboteanu
  • 8,008
  • 1
  • 45
  • 38
0

First of all see, whether you are not override onRestart method and refreshing it.

If you will refresh the activity the image will not set.

user49439
  • 11
  • 3