如何获取图片资源名称

3

我如何获取已动态设置的ImageView资源名称?

以下是图片适配器代码:

  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 };
}
}

1
你能澄清一下“动态设置”是什么意思吗? - jk.
1
你能详细说明一下你的问题吗? - Chirag
4个回答

6
你可以使用 setTag()getTag() 来设置或获取与图像一起的图像资源名称。
当你动态设置图像时,可以添加以下行来设置图像资源名称:
imageView.setTag("image resource name");

如果您想检索图像资源名称,可以使用以下方法

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

实际上,我在使用GridView,并且只使用了一个ImageView并通过ImageAdapter类设置了不同的图像。那么我该如何获取可绘制对象的名称? - VG108
您正在设置图像资源ID,该ID是整数,因此getTag()将返回该整数。您可以通过将该整数值传递给getResources().getDrawable(id)来获取可绘制对象,但不是该图像的名称。 - Sunil Kumar Sahoo
在“getResources().getDrawable(id)”中应该放什么id?当我用R.id.imagview替换id时,我遇到了强制关闭的问题。 - VG108
尝试使用以下代码:Integer id = (Integer) imageView.getTag(); getResources().getDrawable(id); - Sunil Kumar Sahoo
但是在您之前提供的解决方案中,我需要在getDrawable(id)中提供什么“id”? - VG108
与我现在写的一样。 - Sunil Kumar Sahoo

1
你可以使用这段代码。这段代码对我来说运行良好。
String resName = getResourceNameFromClassByID(R.drawable.class, R.drawable.imagename);

而这种方法是

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

更多细节,请参考http://www.anddev.org/viewtopic.php?t=506


链接已损坏或为垃圾邮件! - Noor Hossain
Field 的导入是什么?这个回答不完整。 - Noor Hossain

1
尝试一下
String name = getResources().getResourceEntryName(image[position]);

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

imgvw.setTag("name");

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

这如何返回在ImageView上的资源集? - Ken Herbert
这只会返回你在这里设置的标签名称imgvw.setTag("name"),那么如何获取图像或资源名称呢? - Abhijit Chakra

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接