无法将字符串资源名称转换为资源
unable to convert string resource name into resource
我有一个字符串数组(用于声音文件名),并且我有作为资源(原始)附加到项目的确切文件,我正在尝试通过代码获取这些资源的 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
您需要在没有文件类型扩展名的情况下查询它们:
String[] phrasesDesc = {"doyouspeakenglish", "goodevening", "hello", "howareyou", "ilivein", "mynameis", "please", "welcome"};
或以某种方式删除文件类型扩展名,例如:res_name.replace(".m4a", "")
.
我有一个字符串数组(用于声音文件名),并且我有作为资源(原始)附加到项目的确切文件,我正在尝试通过代码获取这些资源的 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
您需要在没有文件类型扩展名的情况下查询它们:
String[] phrasesDesc = {"doyouspeakenglish", "goodevening", "hello", "howareyou", "ilivein", "mynameis", "please", "welcome"};
或以某种方式删除文件类型扩展名,例如:res_name.replace(".m4a", "")
.