如何从SD卡所有子文件夹访问所有的MP3文件? [英] How to access all mp3 files from all the subfolders in the sdcard?

查看:134
本文介绍了如何从SD卡所有子文件夹访问所有的MP3文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个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。);
    }
}}
 

解决方案

还有就是<一个href="http://www.androidside.com/docs/resources/samples/RandomMusicPlayer/src/com/example/android/musicplayer/MusicRetriever.html"相对=nofollow> 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"));
    }
}  }

解决方案

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

这篇关于如何从SD卡所有子文件夹访问所有的MP3文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆