[android] How do I get the resource id of an image if I know its name?

How do I get the resource id of an image if I know its name (in Android)?

This question is related to android android-image android-resources

The answer is


Example for a public system resource:

// this will get id for android.R.drawable.ic_dialog_alert
int id = Resources.getSystem().getIdentifier("ic_dialog_alert", "drawable", "android");

alert

Another way is to refer the documentation for android.R.drawable class.


You can also try this:

try {
    Class res = R.drawable.class;
    Field field = res.getField("drawableName");
    int drawableId = field.getInt(null);
}
catch (Exception e) {
    Log.e("MyTag", "Failure to get drawable id.", e);
}

I have copied this source codes from below URL. Based on tests done in this page, it is 5 times faster than getIdentifier(). I also found it more handy and easy to use. Hope it helps you as well.

Link: Dynamically Retrieving Resources in Android


One other scenario which I encountered.

String imageName ="Hello" and then when it is passed into getIdentifier function as first argument, it will pass the name with string null termination and will always return zero. Pass this imageName.substring(0, imageName.length()-1)


With something like this:

String mDrawableName = "myappicon";
int resID = getResources().getIdentifier(mDrawableName , "drawable", getPackageName());

You can use this function to get a Resource ID:

public static int getResourseId(Context context, String pVariableName, String pResourcename, String pPackageName) throws RuntimeException {
    try {
        return context.getResources().getIdentifier(pVariableName, pResourcename, pPackageName);
    } catch (Exception e) {
        throw new RuntimeException("Error getting Resource ID.", e)
    } 
}


So if you want to get a Drawable Resource ID, you can call the method like this:

getResourseId(MyActivity.this, "myIcon", "drawable", getPackageName());

(or from a fragment):

getResourseId(getActivity(), "myIcon", "drawable", getActivity().getPackageName());


For a String Resource ID you can call it like this:

getResourseId(getActivity(), "myAppName", "string", getActivity().getPackageName());

etc...


Careful: It throws a RuntimeException if it fails to find the Resource ID. Be sure to recover properly in production.

Read this


Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to android-image

Saving and Reading Bitmaps/Images from Internal memory in Android How to create a circular ImageView in Android? "Bitmap too large to be uploaded into a texture" Reading an image file into bitmap from sdcard, why am I getting a NullPointerException? Open an image using URI in Android's default gallery image viewer Android Text over image How to get Bitmap from an Uri? How do I get the resource id of an image if I know its name? How to clear an ImageView in Android? How to pick an image from gallery (SD Card) for my app?

Examples related to android-resources

Failed linking file resources Execution failed for task ':app:processDebugResources' even with latest build tools getResources().getColor() is deprecated getColor(int id) deprecated on Android 6.0 Marshmallow (API 23) Android getResources().getDrawable() deprecated API 22 Android Studio cannot resolve R in imported project? Android XXHDPI resources Android ImageView setImageResource in code format statement in a string resource file Lint: How to ignore "<key> is not translated in <language>" errors?