Skip to content Skip to sidebar Skip to footer

How To Get Image Name From Drawable Object?

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.ge

Solution 1:

You can try like this:

intimageid= getResources().getIdentifier("image_name", "drawable", getPackageName());
StringimageName= getResources().getResourceName(imageid);

Solution 2:

Stringname= 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);

publicString getResourceNameFromClassByID(Class<?> aClass, intresourceID)
            throwsIllegalArgumentException{
        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();
            }
        }
        thrownew IllegalArgumentException();
    }

Post a Comment for "How To Get Image Name From Drawable Object?"