ID found by Resource Name

Source: Internet
Author: User

The following code loads wallpaper in wallpaperchooser. Java,

1. Write the image name to R. array. wallpaper and save the name list of a wallpaper.

2. After the wallpaper name list is taken out, use

Stirng packagename = res. getresourcepackagename (R. array. Wallpaper), get packagename

Int id = res. getidentifiier (name, "drawable", packagename); get the id value

 private void findWallpapers() {        mThumbs = new ArrayList<Integer>(24);        mImages = new ArrayList<Integer>(24);        final Resources resources = getResources();        // Context.getPackageName() may return the "original" package name,        // com.android.launcher2; Resources needs the real package name,        // com.android.launcher. So we ask Resources for what it thinks the        // package name should be.        final String packageName = resources.getResourcePackageName(R.array.wallpapers);        addWallpapers(resources, packageName, R.array.wallpapers);        addWallpapers(resources, packageName, R.array.extra_wallpapers);    }    private void addWallpapers(Resources resources, String packageName, int list) {        final String[] extras = resources.getStringArray(list);        for (String extra : extras) {            int res = resources.getIdentifier(extra, "drawable", packageName);            if (res != 0) {                final int thumbRes = resources.getIdentifier(extra + "_small",                        "drawable", packageName);                if (thumbRes != 0) {                    mThumbs.add(thumbRes);                    mImages.add(res);                    // Log.d(TAG, "addWallpapers: [" + packageName + "]: " + extra + " (" + res + ")");                }            }        }    }

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.