如何从 android phone 获取所有音乐文件的列表
How to get list of all music files from android phone
我创建了一个可以扫描手机上的音乐文件的应用程序 phone。如果我扫描文件夹 /sdcard/Music,它会扫描音乐文件,但当我写入 /sdcard 时,它不会扫描 mp3 文件。它工作正常。但它在某些设备上不起作用,应用程序崩溃可能是因为该路径不存在 phone 我应该怎么做?
代码
public class SongsManager {
// SDCard Path
String MEDIA_PATH = new String("/sdcard/Music");
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
// Constructor
public SongsManager() {
}
/**
* Function to read all mp3 files from sdcard
* and store the details in ArrayList
*/ public ArrayList<HashMap<String, String>> getPlayList(){
File home = new File(MEDIA_PATH);
if (home.listFiles(new FileExtensionFilter()).length > 0) {
for (File file : home.listFiles(new FileExtensionFilter())) {
HashMap<String, String> song = new HashMap<String, String>();
song.put("songTitle", file.getName().substring(0, (file.getName().length() - 4)));
song.put("songPath", file.getPath());
// Adding each song to SongList
songsList.add(song);
}
}
// return songs list array
return songsList;
}
class FileExtensionFilter implements FilenameFilter {
public boolean accept(File dir, String name) {
return (name.endsWith(".mp3") || name.endsWith(".MP3"));
}
}
}
//通过 URI 检索媒体存储数据库中当前列出的音乐文件列表。
//某些音频可能被明确标记为不是音乐
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
String[] projection = {
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.DURATION
};
cursor = this.managedQuery(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
projection,
selection,
null,
null);
private List<String> songs = new ArrayList<String>();
while(cursor.moveToNext()){
songs.add(cursor.getString(0) + "||" + cursor.getString(1) + "||" + cursor.getString(2) + "||" + cursor.getString(3) + "||" + cursor.getString(4) + "||" + cursor.getString(5));
}
我创建了一个可以扫描手机上的音乐文件的应用程序 phone。如果我扫描文件夹 /sdcard/Music,它会扫描音乐文件,但当我写入 /sdcard 时,它不会扫描 mp3 文件。它工作正常。但它在某些设备上不起作用,应用程序崩溃可能是因为该路径不存在 phone 我应该怎么做?
代码
public class SongsManager {
// SDCard Path
String MEDIA_PATH = new String("/sdcard/Music");
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
// Constructor
public SongsManager() {
}
/**
* Function to read all mp3 files from sdcard
* and store the details in ArrayList
*/ public ArrayList<HashMap<String, String>> getPlayList(){
File home = new File(MEDIA_PATH);
if (home.listFiles(new FileExtensionFilter()).length > 0) {
for (File file : home.listFiles(new FileExtensionFilter())) {
HashMap<String, String> song = new HashMap<String, String>();
song.put("songTitle", file.getName().substring(0, (file.getName().length() - 4)));
song.put("songPath", file.getPath());
// Adding each song to SongList
songsList.add(song);
}
}
// return songs list array
return songsList;
}
class FileExtensionFilter implements FilenameFilter {
public boolean accept(File dir, String name) {
return (name.endsWith(".mp3") || name.endsWith(".MP3"));
}
}
}
//通过 URI 检索媒体存储数据库中当前列出的音乐文件列表。
//某些音频可能被明确标记为不是音乐
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
String[] projection = {
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.DURATION
};
cursor = this.managedQuery(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
projection,
selection,
null,
null);
private List<String> songs = new ArrayList<String>();
while(cursor.moveToNext()){
songs.add(cursor.getString(0) + "||" + cursor.getString(1) + "||" + cursor.getString(2) + "||" + cursor.getString(3) + "||" + cursor.getString(4) + "||" + cursor.getString(5));
}