简体   繁体   中英

How to read certain files from raw folder?

I have created a simple activity for playing a list of music stored in the raw folder but I wanted to create another activities for example, MainActivity - songs from 2008, Other Activity 1 - songs from 2009, Other Activity 2 - songs from 2010, etc. So How do I only read certain files from the raw folder Or is it possible to create sub-directories or read from other new folders?

Here are the codes I have done so far:

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();
            }
        });
    }
}

Question A: Fix: When you create All Audio Files in /resources folder or raw folder(similar), create audio files with name 2008 and append by some string and current timestamp. So, all files for 2008 year will have the string "2008" in /resources folder. So, to retrieve parse the String and retrieve audio files which contain the string "2008", Similarly for 2009 and 2010 years. Reading files from Raw folder: Fix: using resources.getBundle() in java or create file with properties class in default directory which is src/resources folder and using library InputStream or FileInputStream. Is it possible to create folders and sub folders in /resources folder? Yes its possible with Java's mkdir. But keep in mind to create folder names with string like "2008" dynamically appended by current timestamp. and while retrieval, retrieve all files in respective folders which has names like "2008|, "2009" etc. If Filenames become too recursive and tedious, may be some algorithm that has random string can be used and with fixed length. ofcourse, we can put all permutations and combinations of strings to best use to keep number of strings(filenames)(reusable permutation of filenames) to minimum. Hope i answered your question.

There are few steps to complete the task:

  1. Create a Model Class
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. Create the following function in your activity
 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 } }

Now to read the specific files from the raw folder call the function as

listRawFiles("songFrom2008_");

here I suppose that your raw folder has songs with the names like songFrom2008_1,songFrom2008_2..., songFrom2010_1... and so on the list.

To call the list of songs from 2010 use,

listRawFiles("songFrom2010_");

Inside your musicListView.setOnItemClickListener , you can use:

Model model = arrayList.get(i); //arrayList is Model type
musicplayer = MediaPlayer.create(MainActivity.this, model.getResId());
musicplayer.start();

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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