img=(ImageView)findViewById(R.id.picture);
ApplicationInfo appInfo = getApplicationInfo();
int resID = getResources().getIdentifier("hubei.jpg", "drawable", appInfo.packageName);
img.setImageResource(resID);
我这样写的,但是resID总是0,不能用img.setImageResource(R.drawable.hubei);
不要带后续名。 比如你这里就是 "hubei",另外如果还不成功,则检查你的packageName是否正确。 建议参考 launcher 的 wallpaperChooser,我摘一点代码你给参考:
[code="java"]
private void findWallpapers() {
mThumbs = new ArrayList(24);
mImages = new ArrayList(24);
final Resources resources = getResources();
// Context.getPackageName() may return the "original" package name,
// com.fmsoft.launcher2; Resources needs the real package name,
// com.fmsoft.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);
}
}
}
}[/code]