-1

I need set image resource by Send parameter (String src)

I have this code

public void getSource(View view , String src){

    ImageView image = (ImageView)findViewById(R.id.img);
    image.setImageURI(src);
}

How do I solve this problem ؟

3 Answers3

5

// method to call to set image

   private void setImage(int src) {
       ImageView iv = findViewById(R.id.your_image_view);
       iv.setImageResource(src);
   }

//Pass your resource and use it like this :-

setImage(R.drawable.plus_active);
Rishabh Bhatia
  • 1,019
  • 6
  • 14
1

If you want to set image from local storage, then you have to get the uri of the image and use setImageUri like this:
public void getSource(View view , String srcImageUri){

ImageView image = (ImageView)findViewById(R.id.img);
image.setImageURI(Uri.parse(src));
} 

If you have image in drawable folder , then you have to set image like this:

image.setImageResource(R.drawable.imageName);

And if you want to set image from web url, then you have to use libraries like picaso or fresco.

Er.Rohit Sharma
  • 696
  • 5
  • 21
1

use getResources().getIdentifier(); (there you can pass the image name String as a parameter to getIdentifier() function) to get the resourceID of the named image and then use setImageResource(resourceID); to set the image to ImageView.

public void getSource(View view , String src){
    int resID = getResources().getIdentifier( src, "drawable", Context.getPackageName());
    ImageView image = (ImageView)findViewById(R.id.img);
    image.setImageResource(resID);
}

you may need to pass the Context as a parameter to your getSource() function.

Tharusha
  • 635
  • 8
  • 25