我想寫一個類,它從整個SD卡獲取所有的MP3文件。但實際上它只能將音頻文件直接放在SD卡上。所以它不搜索槽子文件夾。我想使用Mediastore,但我沒有把它列入數組列表。也許你能幫助我,非常感謝和抱歉beeing一個小白)vinzenz如何從SD卡獲取所有的音頻文件android
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.HashMap;
import de.me.musicplayer.SongsManager.FileExtensionFilter;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
public class SongsManager {
final String MEDIA_PATH = new String("/sdcard/");
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 to filter files which are having .mp3 extension
* */
class FileExtensionFilter implements FilenameFilter {
public boolean accept(File dir, String name) {
return (name.endsWith(".mp3") || name.endsWith(".MP3"));
}
}
}
請參閱此鏈接.. http://stackoverflow.com/questions/10227895/android-songs-fetching-from-sd-card?rq=1 – Hariharan