我试图显示特定文件夹中的音频文件。文件夹由用户选择并添加到arraylist,因此arraylist是动态的。现在,当我查询多个文件夹时,mediaStore不会返回任何结果。我认为问题出在我的selection
上。我知道selectionArgs
中的项目数量应与选择中的?
相匹配,因此我创建了一个功能,但没有运气。希望你们能帮助我。谢谢!
以下是代码:
String[] whereVal;
String selection;
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String[] folderPaths = storage.loadFolderPaths().toArray(new String[storage.loadFolderPaths().size()]);
selection = MediaStore.Audio.Media.DATA +" IN (" + makePlaceholders(folderPaths.length) + ")" ;
whereVal = folderPaths;
String[] projection = {MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.ALBUM_ID};
Cursor cursor = getActivity().getContentResolver().query(uri, projection, selection, whereVal, null);
private String makePlaceholders(int len) {
StringBuilder sb = new StringBuilder(len * 2 - 1);
sb.append("?");
for (int i = 1; i < len; i++)
sb.append(",?");
Log.e("len: ", String.valueOf(len));
Log.e("Selection: ",sb.toString());
return sb.toString();
}
答案 0 :(得分:0)
除了我的评论:
// strip trackname
path = path.substring(path.lastIndexOf("/")+1,path.length());
String where = MediaStore.Audio.Media.DATA + " LIKE " + "\'%" +path+"%\'";