35

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,241
  • 4
  • 28
  • 31
  • 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

5 Answers5

78

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
  • 41,901
  • 18
  • 127
  • 145
Norfeldt
  • 8,272
  • 23
  • 96
  • 152
0

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

Christine
  • 5,617
  • 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
  • 9,404
  • 1
  • 52
  • 40
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
0

In your OnActivityResult, when you get file uri, you can convert it to bitmap and set bitmap to imageview.(Kotlin way)

val file_uri = data.data
val bitmap = MediaStore.Images.Media.getBitmap(contentResolver, file_uri)
binding.ivJob.setImageBitmap(bitmap)

PS: getBitmap() is deprecated now.