我想创建仅显示包含音乐的文件夹的扫描仪。我只知道如何使用MediaStore创建用于从路径获取音乐的查询
public static QueryTask buildFileQuery(String path, String[] projection)
{
// It would be better to use selectionArgs to pass path here, but there
// doesn't appear to be any way to pass the * when using it.
StringBuilder selection = new StringBuilder();
selection.append("_data GLOB ");
DatabaseUtils.appendEscapedSQLString(selection, path);
// delete the quotation mark added by the escape method
selection.deleteCharAt(selection.length() - 1);
selection.append("*' AND is_music!=0");
Uri media = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
QueryTask result = new QueryTask(media, projection, selection.toString(), null, DEFAULT_SORT);
result.type = TYPE_FILE;
return result;
}
答案 0 :(得分:1)
MediaStore.Audio.Media.DATA
列包含文件路径
Cursor c = getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DATA},
MediaStore.Audio.Media.IS_MUSIC + "=1",
null,
MediaStore.Audio.Media.DATA
);
if (c.moveToFirst()) {
do {
Log.d("T", c.getString(0) + " -- " + c.getString(1));
} while (c.moveToNext());
}
这应该输出以下内容:
Come On -- /storage/emulated/0/Music/The Rolling Stones/More Hot Rocks Big Hits & Fazed Cookies (Disc 2)/10 Come On.mp3
...
有了路径后,您可以恢复文件夹层次结构