【问题标题】:List of Mp3 files from a specific folder - Android来自特定文件夹的 Mp3 文件列表 - Android
【发布时间】:2014-09-05 09:33:05
【问题描述】:

我如何从特定文件夹中获取所有 Mp3 文件的列表?

我的文件夹是

 FILE_PATH = Environment.getExternalStorageDirectory().getPath() + "/my folder/"

以下代码显示所有文件夹中的所有 mp3 .. 我只想从我的文件夹中播种 mp3

 Cursor createCursor(String filter) {
    ArrayList<String> args = new ArrayList<String>();
    String selection;

    if (mShowAll) {
        selection = "(_DATA LIKE ?)";
        args.add("%");
    } else {
        selection = "(";
        for (String extension : CheapSoundFile.getSupportedExtensions()) {
            args.add("%." + extension);
            if (selection.length() > 1) {
                selection += " OR ";
            }
            selection += "(_DATA LIKE ?)";
            selection  = selection  + "AND (IS_MUSIC=1)";
        }
        selection += ")";

        selection = "(" + selection + ") AND (_DATA NOT LIKE ?)";
        args.add("%espeak-data/scratch%");
    }

    if (filter != null && filter.length() > 0) {
        filter = "%" + filter + "%";
        selection =
            "(" + selection + " AND " +
            "((TITLE LIKE ?) OR (ARTIST LIKE ?) OR (ALBUM LIKE ?)))";
        args.add(filter);
        args.add(filter);
        args.add(filter);
    }

【问题讨论】:

标签: android mp3 directory


【解决方案1】:

使用以下代码从所需文件夹中获取 mp3 文件列表:-

private ArrayList<String> listmp3 = new ArrayList<String>();
    String[] extensions = { "mp3" };

    private void loadmp3(String YourFolderPath) {

        File file = new File(YourFolderPath);
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            if (files != null && files.length > 0) {
                for (File f : files) {
                    if (f.isDirectory()) {
                        loadmp3(f.getAbsolutePath());
                    } else {
                        for (int i = 0; i < extensions.length; i++) {
                            if (f.getAbsolutePath().endsWith(extensions[i])) {
                                listmp3.add(f.getAbsolutePath());
                            }
                        }
                    }
                }
            }
        }

    }

【讨论】:

    【解决方案2】:

    这个很好用

     Cursor createCursor(String filter) {
        ArrayList<String> args = new ArrayList<String>();
        String path = Environment.getExternalStorageDirectory().getPath();
    
        String selection = MediaStore.Audio.Media.IS_MUSIC + " !=" + 0
                + " AND " + MediaStore.Audio.Media.DATA + " LIKE '" + path
                + "/Fast Mp3 Downloader/%'";
    
    
    
        if (filter != null && filter.length() > 0) {
            filter = "%" + filter + "%";
            selection =
                "(" + selection + " AND " +
                "((TITLE LIKE ?) OR (ARTIST LIKE ?) OR (ALBUM LIKE ?)))";
            args.add(filter);
            args.add(filter);
            args.add(filter);
        }
    
        String[] argsArray = args.toArray(new String[args.size()]);
    
        getExternalAudioCursor(selection, argsArray);
        getInternalAudioCursor(selection, argsArray);
    
        Cursor c = new MergeCursor(new Cursor[] {
                getExternalAudioCursor(selection, argsArray),
                getInternalAudioCursor(selection, argsArray)});
        startManagingCursor(c);
        return c;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-04
      • 1970-01-01
      • 1970-01-01
      • 2013-02-22
      • 1970-01-01
      相关资源
      最近更新 更多