45

If I get the image name as a variable like following:

var imageName = SERVICE.getImg();

Then, how can I get the resource with R.drawable.????, I tried R.drawable[imageName], but it failed. Any suggestions?

1

5 Answers 5

63
int id = getResources().getIdentifier(imageName, type, package);

This will get you the ID of the resource you are looking for. With it, you can then access the resource from the R class.

Using only the name parameter:

You can also include all the 3 info in the "name" parameter using the following format: "package:type/image_name", something like:

int id = getResources().getIdentifier("com.my.app:drawable/my_image", null, null);

This is useful when you're working with external components or libraries that you can't, or don't want to, change how getIdentifier() is called. e.g.: AOSP Launcher3

5
  • @nicholas, I got resource not found exception. If I have a image named "icon1.gif" in folder "drawable", isn't it getIdentifier("icon1", "gif", "drawable") ??
    – Leem
    Jul 5, 2011 at 14:13
  • Did you look at the link? I think it would be ("icon1", "drawable", "the.containing.package") Jul 5, 2011 at 14:15
  • @nicholas, what does the.containing.package mean??? Isn't the containing package = "projectName/res/drawable" ??
    – Leem
    Jul 5, 2011 at 14:28
  • @Leem: the package is your projects package. Jul 5, 2011 at 14:39
  • make sure to leave the extension (.png or .jpg) out of the imageName String, else resourceId will be set to 0x0 integer which refers to No such resources found Jul 6, 2018 at 14:44
41

Try this:

int id = getResources().getIdentifier(imageName, "drawable", getPackageName());
4
  • 2
    As for R.id.??? equivalent, it will be int id = getResources().getIdentifier(imageName, "id", getPackageName()); Jul 10, 2017 at 20:41
  • It comes at a cost though, if you ever try to Refactor -> Remove un-used resources, it will delete the resource you dynamically access. So unless it's absolutely necessary, better not do it.
    – Irshu
    Aug 2, 2018 at 6:31
  • Can confirm this works for me, although my code was int resourceId = context.getResources().getIdentifier(layoutName, "layout", context.getPackageName()); Mar 3, 2019 at 21:38
  • perfect answer.. Thank you !
    – tenTen
    Feb 11, 2021 at 18:39
15

You need reflection.

Suppose you have R.drawable.image1, if you wanna access it via the String name "image1", following should work:

String Name = "image1";
int id = R.drawable.class.getField(Name).getInt(null);

But notice it only get the Id of the image, you still need the inflater to get the actual drawable from it.

2
  • 1
    Why does this work when the other getIdentifier() methods fail? Dec 12, 2013 at 5:57
  • Fine but didn't work, when minifyEnabled = true . Jul 22, 2023 at 2:00
3

Use this function

public static String getResourceString(String name, Context context) {
    int nameResourceID = context.getResources().getIdentifier(name,
            "string", context.getApplicationInfo().packageName);
    if (nameResourceID == 0) {
        throw new IllegalArgumentException(
                "No resource string found with name " + name);
    } else {
        return context.getString(nameResourceID);
    }
}
0

You can use the getIdentifier method which will give you the resource id by its name. Check this thread for more details.

3
  • what is parameter "defPackage" in getIdentifier()??? Isn't it "projectName/res/drawable" ??
    – Leem
    Jul 5, 2011 at 14:31
  • defPackage - Optional default package to find, if "package:" is not included in the name. Can be null to require an explicit package. Your name should look something like this org.anddev.android.testproject:drawable/bug where bug is the drawable name and org.anddev.android.testproject is your main package name. Please read the article !!
    – Mojo Risin
    Jul 5, 2011 at 14:43
  • Second link in answer is outdated.
    – Pang
    Jun 21, 2023 at 1:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.