如何从SD卡所有子文件夹访问所有的MP3文件?有的、文件夹、文件、SD

由网友(青春是场无悔的旅行)分享简介:我试图做一个MP3播放器应用程序,当我从我的手机上运行它,只读取MP3的是在SD卡上present。它不会从是present存储卡中的子文件夹读取任何MP3的。我希望它显示所有的MP3的present在SD卡(包括子文件夹)。公共类SongsManager {// SD卡路径最后弦乐MEDIA_PATH =新的St...

我试图做一个MP3播放器应用程序,当我从我的手机上运行它,只读取MP3的是在SD卡上present。它不会从是present存储卡中的子文件夹读取任何MP3的。我希望它显示所有的MP3的present在SD卡(包括子文件夹)。

 公共类SongsManager {
// SD卡路径
最后弦乐MEDIA_PATH =新的String(Environment.getExternalStorageDirectory()getPath());
私人的ArrayList< HashMap的<字符串,字符串>> songsList =新的ArrayList< HashMap的<字符串,字符串>>();

//构造函数
公共SongsManager(){

}

/ **
 *功能读取SD卡所有的MP3文件
 *并存储在ArrayList中的细节
 * * /
公众的ArrayList< HashMap的<字符串,字符串>> getPlayList(){
    文件家庭=新的文件(MEDIA_PATH);

    如果(home.listFiles(新FileExtensionFilter())长度GT; 0){
        对于(文件文件:home.listFiles(新FileExtensionFilter())){
            HashMap的<字符串,字符串>歌=新的HashMap<字符串,字符串>();
            song.put(songTitle,file.getName()子串(0,(file.getName()长度() -  4))。。);
            song.put(songPath,file.getPath());

            //添加每一首歌曲的SongList
            songsList.add(歌曲);
        }
    }
    //返回歌曲列表排列
    返回songsList;
}


/ **
 *分类过滤,它们是具有.MP3扩展名的文件
 * * /
类FileExtensionFilter实现了FilenameFilter {
    公共布尔接受(文件目录,字符串名称){
        返程(name.endsWith(MP3)|| name.endsWith()MP3。);
    }
}}
 

解决方案

还有就是 MusicRetriever 在Android SDK中的例子。它采用了 ContentResolver的。

I'm trying to make an mp3 player app, and when I run it from my phone it only reads MP3's that are present on the SD card itself. It does not read any MP3's from the subfolders that are present in the card. I want it to display all the MP3's present in the SD card(including the subfolders).

public class SongsManager {
// SDCard Path
final String MEDIA_PATH = new String(Environment.getExternalStorageDirectory().getPath());
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"));
    }
}  }
android开发 ,循环遍历得到sd卡子文件夹内的MP3文件可以加载到listview中,但点击无法播放

解决方案

There is the MusicRetriever example in the Android SDK. It uses a ContentResolver.

阅读全文

相关推荐

最新文章