简体   繁体   English

原始文件夹中带有字符串的参考文件

[英]Reference files in raw folder with a String

i was searching for a solution how to reference a .mp3 file in my raw folder via a String. 我正在寻找一种解决方案,该方法如何通过字符串引用原始文件夹中的.mp3文件。

Exactly I want to loop through the raw files which are all named player1.mp3, player2.mp3 and so on and store the resource id in an int array, which should contain the ids in the right player order. 确实,我想遍历所有名为player1.mp3, player2.mp3等的原始文件,并将资源ID存储在一个int数组中,该数组应按正确的播放器顺序包含这些ID。

My thought was using something like this: 我的想法是使用这样的东西:

int[] playerArray = new int[100];
String filename;
for(int i = 0; i<100; i++){
   filename = "player"+String.valueOf(i);
   playerArray = R.raw.filename; //I know, this does not work
}

Can anyone tell me an easy way to save the fileids in the right order in an array, and if there is no file with playerX.mp3 just write a 200 in playerArray[X] 谁能告诉我一种简单的方法来以正确的顺序将文件ID保存在数组中,如果没有使用playerX.mp3文件,只需在playerArray[X]写入200

Thanks in forward ;) 谢谢前进;)

You can maybe use this code: 您可以使用以下代码:

int rawId = context.getResources().getIdentifier(filename, "raw", context.getPackageName());

With the method getResources() you can get the id of all the ressource. 使用方法getResources(),您可以获取所有资源的ID。 The second paramter is the type of the ressource (string, drawable, raw, ...) 第二个参数是资源的类型(字符串,可绘制,原始,...)

Your code will be something like that: 您的代码将是这样的:

int[] playerArray = new int[100];
String filename;
for(int i = 0; i<100; i++){
   filename = "player"+String.valueOf(i);
   int rawId = context.getResources().getIdentifier(filename, "raw", context.getPackageName());
   playerArray[i] = rawId;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM