我需要显示保存在 res/drawable 文件夹下的图像。我从 Web 服务调用中获取图像名称,并尝试使用以下代码在可绘制文件夹中找到该图像
ImageView backgroundImage = (ImageView) findViewById(R.id.ivFormBackgroundImage);
Resources res = getResources();
String mDrawableName = formSummary.backgroundImageName;
int resID = res.getIdentifier(mDrawableName , "drawable", getPackageName());
Drawable drawable = res.getDrawable(resID );
backgroundImage.setImageDrawable(drawable );
resID 总是返回 0,我想知道是不是因为包名。我在 Android_Manifest 文件中将我的包名称设置为 package="com.x.y.z.classes.ui.screens",但图像在 res/drawable 中。非常感谢任何有关如何获取图像路径的帮助。 谢谢...
最佳答案
看起来您做得对,但请确保您指定的包名称与实际 R 类所在的包名称匹配。
关于android - res.getIdentifier - 查找包名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6036353/