Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.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

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:如何获取Android资源的资源id_Android_Resources - Fatal编程技术网

Android:如何获取Android资源的资源id

Android:如何获取Android资源的资源id,android,resources,Android,Resources,我想获取保存在/res/raw文件夹中的android资源的资源id 使用— 1) 我正在将资源的名称传递给类,该名称保存在字符串fileNeme中 2) 获取此文件的语句是this.getResources().openrawsource(R.raw.xyz),其中xyz是实际的文件名 3) 我知道openRawResource(int)方法采用int值,因此我不能将语句用作this.getResources().openRawResource(R.raw.filename),其中filena

我想获取保存在/res/raw文件夹中的android资源的资源id

使用—

1) 我正在将资源的名称传递给类,该名称保存在字符串fileNeme中

2) 获取此文件的语句是
this.getResources().openrawsource(R.raw.xyz)
,其中xyz是实际的文件名

3) 我知道openRawResource(int)方法采用int值,因此我不能将语句用作
this.getResources().openRawResource(R.raw.filename)
,其中filename是上面定义的字符串变量

问题:我知道以名为filename的字符串保存的文件名,但我无法打开它,因为该方法采用int格式的资源id

我需要获取此文件名的资源id,并将其保存在int变量filename_int中,然后将此变量传递给openRawResource方法


请让我知道如何才能做到这一点

您应该能够使用
getIdentifier()
获取资源id:


非常感谢。为了节省下一个人的时间,“xyz”参数正是您想要用于xml文件的字符。在那之前或之后你都没有得到任何东西。没有“res/”或“@raw”等。。只需清洁xyzAlso,“xyz”不应包含任何后缀。e、 g.原始资源myfile.json将只是“myfile”。@silver\u man Bingo!这是修复我的问题的注释:-)对于drawables
this.getResources().getIdentifier(“ic_拼写检查_white_24dp”,“drawable”,this.getPackageName())
int id = this.getResources().getIdentifier("xyz", "raw", this.getPackageName());