无法将字符串资源名称转换为资源

时间:2020-04-27 07:59:32

标签: java android android-resources

我有一个字符串数组(用于声音文件名),并且我将确切的文件作为资源(原始)附加到了项目中,我试图通过代码获取这些资源的ID,以更改哪个文件(资源)从代码中播放。

  String[] phrasesDesc = {"doyouspeakenglish.m4a",
            "goodevening.m4a",
            "hello.m4a",
            "howareyou.m4a",
            "ilivein.m4a",
            "mynameis.m4a",
            "please.m4a",
            "welcome.m4a"
    };



       int index = 0;
        for (String str : phrasesDesc) {
            String res_name =phrasesDesc[index] ;
            resourceID[index] = this.getResources().getIdentifier(res_name, "raw", this.getPackageName());
            System.out.println(res_name +"               "+ resourceID[index]);
            index++;
        }

请帮助。

enter image description here

1 个答案:

答案 0 :(得分:1)

您需要查询不带文件扩展名的文件:

String[] phrasesDesc = {"doyouspeakenglish", "goodevening", "hello", "howareyou", "ilivein", "mynameis", "please", "welcome"};

或以某种方式去除文件类型扩展名,例如:res_name.replace(".m4a", "")