问题
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