[英]How to read certain files from raw folder?
我创建了一个简单的活动来播放存储在原始文件夹中的音乐列表,但我想创建另一个活动,例如MainActivity
- 2008 年的歌曲,其他活动 1 - 2009 年的歌曲,其他活动 2 - 2010 年的歌曲等.那么我如何只从原始文件夹中读取某些文件或者是否可以创建子目录或从其他新文件夹中读取?
以下是我到目前为止所做的代码:
MainActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.lang.reflect.Field;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ListView musicListView;
ArrayList<String> arrayList;
ArrayAdapter musicAdapter;
MediaPlayer musicplayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
musicListView = findViewById(R.id.musicListView);
arrayList = new ArrayList<String>();
Field[] fields = R.raw.class.getFields();
for (int i= i=0; i<fields.length; i++) {
arrayList.add(fields[i].getName());
}
musicAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, arrayList);
musicListView.setAdapter(musicAdapter);
musicListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if (musicplayer != null) {
musicplayer.release();
}
int resId = getResources().getIdentifier(arrayList.get(i), "raw", getPackageName());
musicplayer = MediaPlayer.create(MainActivity.this, resId);
musicplayer.start();
}
});
}
}
问题 A:修复:当您在 /resources 文件夹或 raw 文件夹(类似)中创建所有音频文件时,创建名称为 2008 的音频文件并附加一些字符串和当前时间戳。 因此,2008 年的所有文件都将在 /resources 文件夹中包含字符串“2008”。 因此,要检索解析字符串并检索包含字符串“2008”的音频文件,同样适用于 2009 年和 2010 年。 从 Raw 文件夹中读取文件:修复:在 java 中使用 resources.getBundle() 或在默认目录(即 src/resources 文件夹并使用库 InputStream 或 FileInputStream)中创建具有属性类的文件。 是否可以在 /resources 文件夹中创建文件夹和子文件夹? 是的,它可以使用 Java 的 mkdir。 但请记住,创建文件夹名称时,会使用当前时间戳动态附加诸如“2008”之类的字符串。 并在检索时,检索各个文件夹中名称为“2008|”、“2009”等的所有文件。如果文件名变得过于递归和乏味,可能是可以使用一些具有随机字符串且长度固定的算法。当然,我们可以将字符串的所有排列和组合充分利用,以将字符串(文件名)(文件名的可重用排列)的数量保持在最低限度。希望我回答了你的问题。
完成任务有几个步骤:
Model
类public class Model implements Serializable { public int resId; public String fname; public Model (int resId, String name){ this.resId = resId; this.fname = name; } public int getResId() { return resId; } public void setResId(int resId) { this.resId = resId; } }
public void listRawFiles(String word){ String type = "raw"; Resources res = getResources(); // Get the list of MP3 files that start with the specified word for (int i = 1; ; i++) { String name = word + i; Log.e("Resource", name); // Get the ID of the resource with the specified name and type int id = res.getIdentifier(name, type, getPackageName()); // If the ID is invalid, we have reached the end of the list if (id == 0) { break; } // Add the resource name to the list Model model = new Model(id,name, "0"); files.add(model); //files is Model type arraylist } }
现在要从原始文件夹中读取特定文件,请将函数调用为
listRawFiles("songFrom2008_");
在这里,我假设您的原始文件夹中的歌曲名称为 songFrom2008_1、songFrom2008_2...、songFrom2010_1... 等等。
要调用 2010 年使用的歌曲列表,
listRawFiles("songFrom2010_");
在您的musicListView.setOnItemClickListener
中,您可以使用:
Model model = arrayList.get(i); //arrayList is Model type
musicplayer = MediaPlayer.create(MainActivity.this, model.getResId());
musicplayer.start();
希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.