如何从 android 中的私人文件夹路径获取音频文件
How to get audio files from Private folder path in android
我正在尝试从 私人文件夹 中检索音乐播放器的音频文件。
下面是我的代码-
public ArrayList<MusicFiles> getAllAudio(Context context){
SharedPreferences preferences = getSharedPreferences(MY_SORT_PREF, MODE_PRIVATE);
String sortOrder = preferences.getString("sorting", "sortByName");
ArrayList<String> duplicate = new ArrayList<>();
albums.clear();
ArrayList<MusicFiles> tempAudioList = new ArrayList<>();
String order = null;
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
switch (sortOrder){
case "sortByName":
order = MediaStore.MediaColumns.DISPLAY_NAME + " ASC";
break;
case "sortByDate":
order = MediaStore.MediaColumns.DATE_ADDED + " ASC";
break;
case "sortBySize":
order = MediaStore.MediaColumns.SIZE + " DESC";
break;
}
String[] projection = {
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media._ID
};
String folder = getApplicationContext().getDir("Songs", MODE_PRIVATE).getAbsolutePath();
String selection = MediaStore.Audio.Media.DATA + " LIKE ? AND " + MediaStore.Audio.Media.DATA + " NOT LIKE ? ";
String[] selectionArgs = new String[]{
"%" + folder + "%",
"%" + folder + "/%/%"
};
Cursor cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, order);
if(cursor != null){
while(cursor.moveToNext()){
String album = cursor.getString(0);
String title = cursor.getString(1);
String duration = cursor.getString(2);
String path = cursor.getString(3);
String artist = cursor.getString(4);
String id = cursor.getString(5);
MusicFiles musicFiles = new MusicFiles(path, title, artist, album, duration, id);
//for checking
Log.e("Path: " + path, "Album : " + album);
tempAudioList.add(musicFiles);
if(!duplicate.contains(album)){
albums.add(musicFiles);
duplicate.add(album);
}
}
cursor.close();
}
return tempAudioList;
}
MediaStore
无权访问 internal storage 应用程序,因此 MediaStore
不会将它们编入索引。
您可以使用普通文件系统 API(例如 File
上的 listFiles()
)来处理内部存储。
请注意,上面的代码片段中不需要 getApplicationContext()
。
我正在尝试从 私人文件夹 中检索音乐播放器的音频文件。
下面是我的代码-
public ArrayList<MusicFiles> getAllAudio(Context context){
SharedPreferences preferences = getSharedPreferences(MY_SORT_PREF, MODE_PRIVATE);
String sortOrder = preferences.getString("sorting", "sortByName");
ArrayList<String> duplicate = new ArrayList<>();
albums.clear();
ArrayList<MusicFiles> tempAudioList = new ArrayList<>();
String order = null;
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
switch (sortOrder){
case "sortByName":
order = MediaStore.MediaColumns.DISPLAY_NAME + " ASC";
break;
case "sortByDate":
order = MediaStore.MediaColumns.DATE_ADDED + " ASC";
break;
case "sortBySize":
order = MediaStore.MediaColumns.SIZE + " DESC";
break;
}
String[] projection = {
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media._ID
};
String folder = getApplicationContext().getDir("Songs", MODE_PRIVATE).getAbsolutePath();
String selection = MediaStore.Audio.Media.DATA + " LIKE ? AND " + MediaStore.Audio.Media.DATA + " NOT LIKE ? ";
String[] selectionArgs = new String[]{
"%" + folder + "%",
"%" + folder + "/%/%"
};
Cursor cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, order);
if(cursor != null){
while(cursor.moveToNext()){
String album = cursor.getString(0);
String title = cursor.getString(1);
String duration = cursor.getString(2);
String path = cursor.getString(3);
String artist = cursor.getString(4);
String id = cursor.getString(5);
MusicFiles musicFiles = new MusicFiles(path, title, artist, album, duration, id);
//for checking
Log.e("Path: " + path, "Album : " + album);
tempAudioList.add(musicFiles);
if(!duplicate.contains(album)){
albums.add(musicFiles);
duplicate.add(album);
}
}
cursor.close();
}
return tempAudioList;
}
MediaStore
无权访问 internal storage 应用程序,因此 MediaStore
不会将它们编入索引。
您可以使用普通文件系统 API(例如 File
上的 listFiles()
)来处理内部存储。
请注意,上面的代码片段中不需要 getApplicationContext()
。