开发者

how to set imageview src?

开发者 https://www.devze.com 2023-03-13 04:20 出处:网络
I have an image view and a string src. I want to set the imageview source to the s开发者_Python百科tring src that Ihave, but am unable to do so beacuse the method expects an int:

I have an image view and a string src. I want to set the imageview source to the s开发者_Python百科tring src that I have, but am unable to do so beacuse the method expects an int:

imgview.setImageResource(int);

Since this method takes an int how can I accomplish my goal of using a string?


Each image has a resource-number, which is an integer. Pass this number to "setImageResource" and you should be ok.

Check this link for further information:
http://developer.android.com/guide/topics/resources/accessing-resources.html

e.g.:

imageView.setImageResource(R.drawable.myimage);


To set image cource in imageview you can use any of the following ways. First confirm your image is present in which format.

If you have image in the form of bitmap then use

imageview.setImageBitmap(bm);

If you have image in the form of drawable then use

imageview.setImageDrawable(drawable);

If you have image in your resource example if image is present in drawable folder then use

imageview.setImageResource(R.drawable.image);

If you have path of image then use

imageview.setImageURI(Uri.parse("pathofimage"));


What you are looking for is probably this:

ImageView myImageView;
myImageView = mDialog.findViewById(R.id.image_id);
String src = "imageFileName"

int drawableId = this.getResources().getIdentifier(src, "drawable", context.getPackageName())
popupImageView.setImageResource(drawableId);

Let me know if this was helpful :)

0

精彩评论

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

关注公众号