【问题标题】:How to read certain files from raw folder?如何从原始文件夹中读取某些文件?
【发布时间】:2022-12-16 06:29:37
【问题描述】:

我创建了一个简单的活动来播放存储在原始文件夹中的音乐列表,但我想创建另一个活动,例如,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();
            }
        });
    }
}

【问题讨论】:

  • 原始文件夹与其他任何文件夹一样资源文件夹 - 您可以通过 ID 访问其中的内容,例如 R.raw.my_thing,因此没有层次结构。您可能想通过 Resources.getAssets().open("songs/2009/macarena.mp3") 使用 assets 文件夹
  • @cactustictacs 我找到了一种使用AssetFileDescriptor 的方法,但它似乎不适用于子文件夹。我也试过没有任何文件夹,但有时播放的音频与代码中提到的不一样,例如getAssets().openFd("macarena.mp3") 但谢谢你,我决定改为按名称过滤它们。

标签: java android android-studio


【解决方案1】:

问题一:修正:当您在 /resources 文件夹或原始文件夹(类似)中创建所有音频文件时,创建名称为 2008 的音频文件并附加一些字符串和当前时间戳。因此,2008 年的所有文件都将在 /resources 文件夹中包含字符串“2008”。因此,要检索解析字符串并检索包含字符串“2008”的音频文件,对于 2009 年和 2010 年也是如此。 从 Raw 文件夹中读取文件:使固定:在 java 中使用 resources.getBundle() 或在默认目录(src/resources 文件夹)中创建具有属性类的文件,并使用库 InputStream 或 FileInputStream。 是否可以在 /resources 文件夹中创建文件夹和子文件夹?是的,使用 Java 的 mkdir 是可能的。但请记住,创建文件夹名称时要使用像“2008”这样的字符串动态附加当前时间戳。并在检索时,检索各个文件夹中名称为“2008|”、“2009”等的所有文件。如果文件名变得过于递归和乏味,可能可以使用一些具有随机字符串和固定长度的算法。当然,我们可以充分利用字符串的所有排列和组合,以将字符串(文件名)(文件名的可重用排列)的数量保持在最低限度。 希望我回答了你的问题。

【讨论】:

  • 谢谢,我试过添加if (fields[i].getName().startsWith("TITLE_HERE")),效果很好。
【解决方案2】:

完成任务有几个步骤:

  1. 创建一个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;
        }
    
    }
    
    1. 在您的活动中创建以下函数
     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();
    

    希望能帮助到你!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多