How to access resource with dynamic name in my case?
Solution 1
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
Solution 2
Try this:
int id = getResources().getIdentifier(imageName, "drawable", getPackageName());
Solution 3
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.
Solution 4
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);
}
}
Related videos on Youtube
Leem
Updated on August 11, 2020Comments
-
Leem almost 4 years
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 triedR.drawable[imageName]
, but it failed. Any suggestions?-
Blundell almost 13 yearsI dont use the NDK hence comment: getResources().getIdentifier (imageName, null , null); API: developer.android.com/reference/android/content/res/…, java.lang.String, java.lang.String)
-
-
Leem almost 13 years@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") ??
-
nicholas.hauschild almost 13 yearsDid you look at the link? I think it would be ("icon1", "drawable", "the.containing.package")
-
Leem almost 13 years@nicholas, what does the.containing.package mean??? Isn't the containing package = "projectName/res/drawable" ??
-
Leem almost 13 yearswhat is parameter "defPackage" in getIdentifier()??? Isn't it "projectName/res/drawable" ??
-
nicholas.hauschild almost 13 years@Leem: the package is your projects package.
-
Mojo Risin almost 13 yearsdefPackage - 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 !!
-
Joshua Pinter over 10 yearsWhy does this work when the other
getIdentifier()
methods fail? -
Neurotransmitter almost 7 yearsAs for
R.id.???
equivalent, it will beint id = getResources().getIdentifier(imageName, "id", getPackageName());
-
Koushik Shom Choudhury almost 6 yearsmake sure to leave the extension (.png or .jpg) out of the
imageName
String, elseresourceId
will be set to0x0
integer which refers to No such resources found -
Irshu almost 6 yearsIt 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. -
Joel Balmer over 5 yearsCan confirm this works for me, although my code was
int resourceId = context.getResources().getIdentifier(layoutName, "layout", context.getPackageName());
-
tenTen over 3 yearsperfect answer.. Thank you !