如何从Drawable Object获取图像名称?

时间:2017-07-04 05:31:01

标签: android image bitmap drawable android-drawable

不知怎的,我有Drawable文件夹中的Drawable图像,如

Drawable thumb = mContext.getResources().getDrawable(R.drawable.image_name);

我试试这个

String image_name =thumb.getCurrent().toString();

但我的需要图片名称,意思是来自拇指的image_name;

怎么可能?

任何建议,评论,咨询;提供有用的链接非常感谢。提前谢谢

2 个答案:

答案 0 :(得分:4)

您可以尝试这样:

int imageid = getResources().getIdentifier("image_name", "drawable", getPackageName());
String imageName = getResources().getResourceName(imageid);

答案 1 :(得分:0)

String name = getResources().getResourceEntryName(R.drawable.image_name);
Log.e("name", name);

String resName = getResourceNameFromClassByID(R.drawable.class, 
R.drawable.image_name);
Log.e("name", resName);

public String getResourceNameFromClassByID(Class<?> aClass, int resourceID)
            throws IllegalArgumentException {
        Field[] drawableFields = aClass.getFields();

        for (Field f : drawableFields) {
            try {

                if (resourceID == f.getInt(null))
                    return f.getName(); // Return the name.
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        throw new IllegalArgumentException();
    }