How to get ImageView Image Image in Android?

I have an image in android. I want to get the name of the image that is set in the image view. How can i do this?

ImageView v = (ImageView)findViewbyId(R.id.img);
//String name = findImageName(v);
if( name.equals(a))
{
   //do something
} 
else{
   //do something
}
+4
source share
4 answers

To do this, first of all you need to set a tag for your ImageView

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageview1" 
    android:background="@drawable/bg1"
    android:tag="bg1"/>

Now get the image name set in your ImageView by doing the following:

ImageView v = (ImageView)findViewbyId(R.id.img);
String backgroundImageName = String.valueOf(v.getTag());

and this should return the image tag name "bg1" .

Now compare tag names or use them for further process

if( backgroundImageName.equals(bg1))
{
   //do something
} 
else{
   //do something
}
+5
source

You can set the ussing name tag.

imageView.setTag("any name")

And get the value using

String imageNameName = (String) imageView.getTag();
+2
source
    ImageView img = (ImageView) findViewById(R.id.imageView);
    String imageName = img.getContentDescription();
+1

- ?

ImageView v = (ImageView)findViewbyId(R.id.img);
if(R.id.someId == v.getId())
{
   //do something
} 
else{
   //do something
}

This is simpler than resource resource names.

+1
source

Source: https://habr.com/ru/post/1599852/


All Articles