0

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

2 Answers2

8

You can try like this:

int imageid = getResources().getIdentifier("image_name", "drawable", getPackageName());
String imageName = getResources().getResourceName(imageid);
0
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();
    }
Pavya
  • 6,015
  • 4
  • 29
  • 42