How to get image name from Drawable Object?

时光怂恿深爱的人放手 提交于 2020-12-16 06:57:44

问题


Somehow I have Drawable image from Drawable folder like as

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

i try like this

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

But my need image name that means image_name from thumb;

How it is possible?

Any suggestion, comment, consults; provide useful links are highly appreciate. Advance Thanks


回答1:


You can try like this:

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



回答2:


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

or

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


来源:https://stackoverflow.com/questions/44897639/how-to-get-image-name-from-drawable-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!