Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set imageview src?

Tags:

android

I have an image view and a string src. I want to set the imageview source to the string 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?

like image 354
sw-dev Avatar asked Jun 16 '11 08:06

sw-dev


People also ask

How do you set the content of an ImageView in your Java code?

ImageView imageView = new ImageView(this); imageView. setImageResource(R. drawable. beerbottle); RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.

How do you set the content of an ImageView in your XML layout file?

Navigate to the app > res > layout > activity_main. xml and add the below code to that file.


2 Answers

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")); 
like image 28
Sunil Kumar Sahoo Avatar answered Oct 18 '22 08:10

Sunil Kumar Sahoo


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); 
like image 88
Select0r Avatar answered Oct 18 '22 09:10

Select0r