Android 如何获取图像资源名称

Android 如何获取图像资源名称,android,Android,如何获取已动态设置的imageview资源名称 这是图像适配器代码: public class ImageAdapter extends BaseAdapter { private Context mContext; public ImageAdapter(Context c) { mContext = c; } public int getCount() { return mThumbIds.length; } public Object ge

如何获取已动态设置的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 };
}
}
您可以使用
setTag()
getTag()
设置或获取图像资源名称以及imgae

动态设置图像时,可以添加以下行以设置图像的imageresource名称

imageView.setTag("image resource name");
如果要检索图像资源名称,可以使用

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

您可以使用此代码。这个代码对我来说很好用

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();
}
公共字符串getResourceNameFromClassByID(类aClass,int resourceID)
抛出IllegalArgumentException{
/*获取传递的类中的所有字段*/
字段[]drawableFields=aClass.getFields();
/*遍历所有字段*/
用于(字段f:可绘图字段){
试一试{
/*R的子类中的所有字段
*是整数,所以这里不需要类型检查*/
/*与我们正在搜索的resourceID进行比较*/
if(resourceID==f.getInt(null))
return f.getName();//返回名称。
}捕获(例外e){
e、 printStackTrace();
}
}
/*如果未找到任何内容,则引发异常*/
抛出新的IllegalArgumentException();
}
有关更多详细信息,请参阅

试试这个
你能解释一下“动态设置”是什么意思吗?你能详细说明你的问题吗?事实上,先生,我使用了网格视图,其中只使用了一个imageview,并在使用imageadapter类时设置了Different image,所以我如何才能获得drawableyou的名称你正在设置图像资源id,它是整数,所以getTag()将返回该整数。您可以通过将该整数值传递给getResources().getDrawable(id),而不是该图像的名称来获取drawable对象。在“getResources().getDrawable(id)”id中放置什么,当我用R.id.imagview替换该id时,我将使用以下代码integer id=(integer)imageView.getTag()获取强制关闭尝试;getResources().getDrawable(id);但是在前面的解决方案中,您已经给出了我在getDrawable(id)“id”中必须给出的内容,它如何返回ImageView上的资源集?这只会给出您在这里设置的标记名imgvw.setTag(“name”);因此,如何获取图像或资源名称。
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();
}
String name = getResources().getResourceEntryName(image[position]);