Use getIdentifier () to obtain the resource Id

Source: Internet
Author: User

In the project, the image resources (regular images) in a directory are read to an array or the image name is read from the database. Then, the image is called and R is used directly. drawable .? Unable to call. Summary: 1. instead of placing images under res/drawable, they are stored in a src package (for example, com. chen. in this case, the call method is: String path = "com/chen/resource/imageName.png"; InputStream is = getClassLoader (). getResourceAsStream (path); Drawable. createFromStream (is, "src"); 2. if you still want to directly use the images in res/drawable, you need to use the following method: assume that the package name entered during project creation is com. test. image int resID = getResources (). getIdentifier ("imageName", "drawable", "com. test. image "); Drawable image = getResources (). getDrawable (resID); Method for reading regular images in the drawable directory cyclically: [java] private Integer [] mImageIds = new Integer [45]; for (int I = 0; I <45; I ++) {int drawable = getResources (). getIdentifier ("image _" + (I + 1), "drawable", this. getPackageName (); mImageIds [I] = drawable ;}

Related Article

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.