Android_ using Getidentifier () to get the resource ID

Source: Internet
Author: User

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);}

    1. 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");

    1. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.