Skip to content Skip to sidebar Skip to footer

How To Get Image Resource Name

How do I get an imageview resource name which has been set dynamically? This is the image Adapter code: public class ImageAdapter extends BaseAdapter { private Context mContext

Solution 1:

you can use setTag() and getTag() to set or get image resource name along with imgae

when you are setting image dynamically you can add the following line to set imageresource name with the image

imageView.setTag("image resource name");

if you want to retrieve image resource name you can use

String imageName = (String) imageView.getTag();

Solution 2:

You can use this code. This code works fine for me.

String resName = getResourceNameFromClassByID(R.drawable.class, R.drawable.imagename);

and the method is

public String getResourceNameFromClassByID(Class<?> aClass, int resourceID)throws IllegalArgumentException{
    /* Get all Fields from the class passed. */
    Field[] drawableFields = aClass.getFields();

    /* Loop through all Fields. */for(Field f : drawableFields){
        try {
            /* All fields within the subclasses of R 
             * are Integers, so we need no type-check here. *//* Compare to the resourceID we are searching. */if (resourceID == f.getInt(null))
                return f.getName(); // Return the name.
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /* Throw Exception if nothing was found*/thrownewIllegalArgumentException();
}

for more details , you can refer to http://www.anddev.org/viewtopic.php?t=506

Solution 3:

Try this

Stringname= getResources().getResourceEntryName(image[position]);

Solution 4:

ImageViewimgvw= (ImageView)findViewById(R.id.imageview1);

imgvw.setTag("name");

StringstrImgName= (String) imgvw.getTag();

Post a Comment for "How To Get Image Resource Name"