Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:使用字符串作为图像名称将图像显示到ImageView(在真实设备中)_Android - Fatal编程技术网

Android:使用字符串作为图像名称将图像显示到ImageView(在真实设备中)

Android:使用字符串作为图像名称将图像显示到ImageView(在真实设备中),android,Android,我无法使用字符串作为图像名称将图像从@drawable加载到ImageView 这是我的密码 String uri = "@drawable/"+imgname; // image file int resID = getResources().getIdentifier(uri , "drawable", getPackageName()); Drawable drawable = getResources().getDrawable(resI

我无法使用字符串作为图像名称将图像从
@drawable
加载到ImageView

这是我的密码

String uri = "@drawable/"+imgname;  // image file
            int resID = getResources().getIdentifier(uri , "drawable", getPackageName());
            Drawable drawable = getResources().getDrawable(resID); //<----line with error
            ImageView imageview= (ImageView)findViewById(R.id.hardImg);
            imageview.setImageDrawable(drawable);
String uri=“@drawable/”+imgname;//图像文件
int resID=getResources().getIdentifier(uri,“可绘制”,getPackageName());
Drawable Drawable=getResources().getDrawable(resID)// 这应该是有效的-
String uri = "@drawable/"+imgname;  // image file with extension, make sure image name is correct.
int resID = getResources().getIdentifier(uri , null, getPackageName());
Drawable drawable = getResources().getDrawable(resID); 
ImageView imageview= (ImageView)findViewById(R.id.hardImg);
imageview.setImageDrawable(drawable);

这应该是有效的-
String uri = "@drawable/"+imgname;  // image file with extension, make sure image name is correct.
int resID = getResources().getIdentifier(uri , null, getPackageName());
Drawable drawable = getResources().getDrawable(resID); 
ImageView imageview= (ImageView)findViewById(R.id.hardImg);
imageview.setImageDrawable(drawable);

您不需要
“@drawable/”
。这就是
getIdentifier
的第二个参数所做的

getResources().getIdentifier(imgname, "drawable", getPackageName());
将为
.R.drawable.
0
生成整数资源ID(如果该资源不存在)

如果未找到此类资源,则返回0。(0不是有效的资源ID。)

因此,您的错误消息

注意:Android资源只能包含某些字符。

您不需要
“@drawable/”
。这就是
getIdentifier
的第二个参数所做的

getResources().getIdentifier(imgname, "drawable", getPackageName());
将为
.R.drawable.
0
生成整数资源ID(如果该资源不存在)

如果未找到此类资源,则返回0。(0不是有效的资源ID。)

因此,您的错误消息



注意:Android资源只能包含某些字符。

删除
“@drawable/”
。您只需要资源名,即文件名,不带扩展名。我只是。。。但仍然有相同的错误。该图像名称是否存在于可绘制文件夹中?是。字符串名称来自ext db,如果它区分大小写,我已经检查了所有大小写。删除
“@drawable/”
。您只需要资源名,即文件名,不带扩展名。我只是。。。但仍然有相同的错误。该图像名称是否存在于可绘制文件夹中?是。字符串名来自ext db,我已经检查了所有大小写是否区分大小写。你是说类似这样的吗<代码>字符串uri=“@drawable/”+imgname+”.png”
@Jay No.给定文件
your_image.png
,您希望
R.drawable.your_image
。Android并不在乎你有一个
png
,所以
@drawable/your_image
才是最好的选择resource@cricket_007我不能
imgname
在单击按钮时保存不同的图像名称。。它会根据此字符串名称更改图像。名称存储在外部数据库中。@Jay您误解了我的评论<代码>“@drawable/”+imgname+”.png“不正确。我就这么说了。@cricket_007我的错。你是说像这样的吗<代码>字符串uri=“@drawable/”+imgname+”.png”@Jay No.给定文件
your_image.png
,您希望
R.drawable.your_image
。Android并不在乎你有一个
png
,所以
@drawable/your_image
才是最好的选择resource@cricket_007我不能
imgname
在单击按钮时保存不同的图像名称。。它会根据此字符串名称更改图像。名称存储在外部数据库中。@Jay您误解了我的评论<代码>“@drawable/”+imgname+”.png“不正确。我就这么说了。@cricket_007我的错。好吧。因此,我制作了这个
stringuri=imgname;int resi=getResources().getIdentifier(uri,“drawable”,getPackageName())但仍有错误.java.lang.RuntimeException:无法启动活动ComponentInfo{com.example.hamira.wordmatchinggame/com.example.hamira.wordmatchinggame.ingamehard}:android.content.res.Resources$NotFoundException:资源ID#0x0Yes。阅读我提供的文档中的报价。我不知道将
imgname
设置为什么,但很明显,该名称在应用程序的
res/drawable
folderI中不存在。我试图给出一个像
imgname=“ac.png”
这样的直接名称。ac.png显然存在于
res/drawable
中,但我仍然有同样的错误:-(我尝试了其他方法,但它出现了!我不明白。好的。因此,我制作了这个
String uri=imgname;int resID=getResources().getIdentifier(uri,“drawable”,getPackageName());
但仍有错误。java.lang.RuntimeException:无法启动活动组件信息{com.example.hamira.wordmatchinggame/com.example.hamira.wordmatchinggame.ingamehard}:android.content.res.Resources$NotFoundException:Resource ID#0x0Yes。请阅读我提供的文档中的引用。我不知道
imgname
设置为什么,但显然该名称不存在于应用程序的
res/drawable
folderI试图给出一个直接名称,如
imgname=“ac.png”
.ac.png显然存在于
res/drawable
中,但我仍然有同样的错误:-(我尝试了其他方法,但它出现了!我不明白。