android - res.getIdentifier - 查找包名

标签 android

我需要显示保存在 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.cl​​asses.ui.screens",但图像在 res/drawable 中。非常感谢任何有关如何获取图像路径的帮助。 谢谢...

最佳答案

看起来您做得对,但请确保您指定的包名称与实际 R 类所在的包名称匹配。

关于android - res.getIdentifier - 查找包名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6036353/

相关文章:

java - 创建具有 IDE 上下文行为的 Java 注解

android.database.sqlite.SQLiteException : no such column: aa: , 编译时:Select Name from stud Where Address=aa

android - FragmentPagerAdapter 中的不同 fragment

android - Gradle Android 项目依赖项不起作用

java - 为什么 "this"用作 RelativeLayout 构造函数中的参数?

android - 在名为 Google map 的程序中

android - 使用 Android 模式锁定屏幕作为登录到我的 android 应用程序

android - 在 flutter 中,底部重载了 248 像素

android - Android 公共(public) API 被删除的任何示例

Android:如果屏幕锁定或在后台,C++ 线程不会唤醒。使用应用程序时工作正常