开发者

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

is related to:

开发者_如何学GoNeed 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


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.


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


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜