Work requires the use of the Getidentifier () method to easily obtain the specified resource ID under each application package.
There are two main ways of doing this:
(1) mode one
Resources resources = Context.getresources ();
int indentify = resources. Getidentifier (Org.loveandroid.androidtest:drawable/icon ", null,null);
if (indentify>0) {
icon = resources.getdrawable (indentify);
}
- The first parameter format is: Package name +: + Resource folder name +/+ resource name; Is this format and the other can be null
(2) mode two
Resources resources = Context.getresources ();
Intindentify= getresources (). Getidentifier ("icon", "drawable", "Org.anddev.android.testproject");
- The first parameter is the ID name, the second is the resource attribute is the ID or the drawable, and the third is the package name.
Read the picture name from the database, and then call the picture. Directly with R.drawable.? cannot be called. Check a lot of places finally found a way to share to everyone, hope to help.
Mainly by two methods, individuals suggest the second kind.
1. Instead of placing the image under Res/drawable, it is stored in a SRC package (for example: Com.drawable.resource), in which case the call method is:
String Path = "Com/drawable/resource/imagename.png";
InputStream is = getClassLoader (). getResourceAsStream (path);
Drawable.createfromstream (IS, "src");
2. If you still want to use the image in res/drawable directly, you need to go through the following method:
Assuming that the project was created, the completed package name is: Com.test.image
int resID = Getresources (). Getidentifier ("ImageName", "drawable", "com.test.image");
drawable image = Getresources (). getdrawable (ResID);
Getidentifier () gets the resource ID