VG108
VG108

Reputation: 122

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;

  public ImageAdapter(Context c) {
    mContext = c;
  }

  public int getCount() {
    return mThumbIds.length;
  }

  public Object getItem(int position) {
    return null;
  }

  public long getItemId(int position) {
    return 0;
  }

  // create a new ImageView for each item referenced by the Adapter
  public View getView(int position, View convertView, ViewGroup parent) {
    View v;

    if (convertView == null) { // if it's not recycled, initialize some
      // attributes

      LayoutInflater li = getLayoutInflater();
      v = li.inflate(R.layout.gridxml, null);
        imageView = (ImageView)v.findViewById(R.id.icon_image);

      imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
      //imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
      imageView.setPadding(5, 5, 5, 5);
    } else {
      imageView = (ImageView) convertView;
  }

    imageView.setImageResource(mThumbIds[position]);
        imageView.setTag(mThumbIds[position]);

    System.out.println(mThumbIds[0]);
          System.out.println(mThumbIds[1]);
          System.out.println(mThumbIds[2]);
          System.out.println(mThumbIds[3]);
          System.out.println(mThumbIds[4]);
    return imageView;
  }

  // references to our images
  private Integer[] mThumbIds = { R.drawable.directory_xml,
      R.drawable.news_xml, R.drawable.calendar_xml,
      R.drawable.facilities_xml, R.drawable.employee_handbook_xml,R.drawable.settings_xml };
}
}

Upvotes: 3

Views: 23397

Answers (4)

Nikhil
Nikhil

Reputation: 16196

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

imgvw.setTag("name");

String strImgName = (String) imgvw.getTag();

Upvotes: -2

Anil M H
Anil M H

Reputation: 3342

Try this

String name = getResources().getResourceEntryName(image[position]);

Upvotes: 1

Rathakrishnan Ramasamy
Rathakrishnan Ramasamy

Reputation: 1612

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*/
    throw new IllegalArgumentException();
}

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

Upvotes: 1

Sunil Kumar Sahoo
Sunil Kumar Sahoo

Reputation: 53657

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();

Upvotes: 6

Related Questions