Another way for Android to get the resource ID is to get the ID of the resource under a specific folder, and if you need another method to get the ID under a special folder:
Use 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= 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 (); int indentify= 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.
Returns the resource ID if found, and returns 0 if it is not found.
Wrote a method: Gets the resource ID if there is no return 0
Static intGetresourceid (Context context,string name,string type,string packagename) {Resources themeresources=NULL; Packagemanager pm=Context.getpackagemanager (); Try{themeresources=pm.getresourcesforapplication (PackageName); returnthemeresources.getidentifier (name, type, packagename); } Catch(namenotfoundexception e) {e.printstacktrace (); } return 0; }
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);D Rawable.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"= Getresources (). getdrawable (ResID);
Android_ using Getidentifier () to get the resource ID