Android - 仅在特定文件夹(不包括子文件夹)上查询音频文件

时间:2015-10-30 04:15:48

标签: android android-sqlite android-contentprovider android-music-player

我正在尝试从特定文件夹上的外部内容URI查询音频文件列表。但子文件夹上的所有音频也包括在内。

如何仅在特定文件夹上获取音频并排除子文件夹。

这就是我试图做的事情。

 Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

    Cursor cursor = contentResolver.query(
            uri, projection,MediaStore.Audio.Media.DATA + " LIKE ? " , new String[]{"%/storage/emulated/0/Music/Tamil/%"} , null);

如果查询错误,请告诉我

我在StackOverflow上尝试了多个答案,但都没有效果。

2 个答案:

答案 0 :(得分:1)

你应该能够这样做:

Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String folder = "/storage/emulated/0/Music/Tamil";
String selection = MediaStore.Audio.Media.DATA + " LIKE ? AND " + MediaStore.Audio.Media.DATA + " NOT LIKE ? ";
String[] selectionArgs = new String[]{
        "%" + folder + "%",
        "%" + folder + "/%/%"
};
Cursor cursor = contentResolver.query(uri, projection, selection, selectionArgs, null);

答案 1 :(得分:0)

试试这种方式

// The folder needs to end with the % wildcard or stuff doesn't work properly.
String folder = "/mnt/sdcard/Music/Some Folder/";
folder = folder + "%";
String where = Artists._ID + " IN (SELECT " + Media.ARTIST_ID + " FROM audio WHERE " +
    Media.DATA + " LIKE ?)";
String[] whereArgs = new String[]{folder};
audioCursor = getContentResolver().query(MediaStore.Audio.Artist.EXTERNAL_CONTENT_URI,
    cols, where, whereArgs, AudioColumns.ARTIST + " COLLATE LOCALIZED ASC");