0
public static File[] filenames;
public static ArrayList<String> shortFilenames;
public static String MyDirectory = "/AudioSamples";


public static ArrayList<String> getFileNames() {
    File sdCardRoot = new File(Environment.getExternalStorageDirectory() + MyDirectory);

        filenames = sdCardRoot.listFiles();
        shortFilenames = new ArrayList<String>();
        for (File tmpf : filenames) {

                String name = tmpf.getName();
                shortFilenames.add(name);

        }
    return shortFilenames;
}

我只需要从 sd 卡上的目录中获取音乐文件(如 m4b、mp3)和包含音乐/声音的文件夹的名称,我该怎么做?我的代码需要一切,所以我需要某种过滤器:)

4

1 回答 1

3
static Uri[] mUris;
static String[] mFiles = null;
public static String[] getMusic(){

    File music = Environment.getExternalStorageDirectory();

    // File asd = Environment.

    // String[] savedFiles = getApplicationContext().fileList();

    File[] musicList = music.listFiles(new FilenameFilter() {

        @Override
        public boolean accept(File dir, String name) {
            // TODO Auto-generated method stub
            return ((name.endsWith(".mp3")||(name.endsWith(".m4b")||(name.endsWith(".flac"))));
        }
    });

    mFiles = new String[musicList.length];
    for(int i=0; i<musicList.length;i++) {
        mFiles[i]=musicList[i].getAbsolutePath();
    }

    return mFiles;
于 2013-09-02T19:27:01.913 回答