如何从SD卡和手机内存中获取.mp3格式的所有歌曲

时间:2016-04-13 16:44:25

标签: java android

我在android中开发了一个音乐播放器应用程序。我有一个叫做歌曲经理的课。这里我将获取所有.mp3文件,但以下代码未从SD卡和手机内存中获取所有.mp3文件。你能帮我吗?

Java文件

public class SongsManager {
    // SDCard Path
    final String MEDIA_PATH = Environment.getExternalStorageDirectory().getPath() + "/";
    private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
    private String mp3Pattern = ".mp3";



// Constructor
    public SongsManager() {

    }

    public ArrayList<HashMap<String, String>> getPlayList() {
        System.out.println(MEDIA_PATH);
        if (MEDIA_PATH != null) {
            File home = new File(MEDIA_PATH);
            File[] listFiles = home.listFiles();
            if (listFiles != null && listFiles.length > 0) {
                for (File file : listFiles) {
                    System.out.println(file.getAbsolutePath());
                    if (file.isDirectory()) {
                        scanDirectory(file);
                    } else {
                        addSongToList(file);
                    }
                }
            }
        }
        // return songs list array
        return songsList;
    }

    private void scanDirectory(File directory) {
        if (directory != null) {
            File[] listFiles = directory.listFiles();
            if (listFiles != null && listFiles.length > 0) {
                for (File file : listFiles) {
                    if (file.isDirectory()) {
                        scanDirectory(file);
                    } else {
                        addSongToList(file);
                    }

                }
            }
        }
    }

    private void addSongToList(File song) {
        if (song.getName().endsWith(mp3Pattern)) {
            HashMap<String, String> songMap = new HashMap<String, String>();
            songMap.put("songTitle",
                    song.getName().substring(0, (song.getName().length() - 4)));
            songMap.put("songPath", song.getPath());

            // Adding each song to SongList
            songsList.add(songMap);
        }
    }
}

我还在清单文件中提供以下内容。请看下面的

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission-sdk-23 android:name="android.permission.READ_EXTERNAL_STORAGE"/>

请帮助我

2 个答案:

答案 0 :(得分:0)

这将为您提供SD卡上的mp3文件列表

 public static void findFilesRecursively(String directoryName, List<File> files, Pattern pattern) {
    File directory = new File(directoryName);

    // get all the files from a directory
    File[] fList = directory.listFiles();
    for (File file : fList) {
        if (file.isFile()) {
            if(pattern.matcher(file.getName()).find()) {
                files.add(file);
            }
        } else if (file.isDirectory()) {
            findFilesRecursively(file.getAbsolutePath(), files, pattern);
        }
    }
    File dir = Environment.getExternalStorageDirectory();
    List<File> fileResult = new ArrayList<File>();
    findFilesRecursively(dir.getAbsolutePath(), fileResult,Pattern.compile(".*\\.mp3$"));

答案 1 :(得分:0)

这很有用。任何人都可以尝试

String media_path = Environment.getExternalStorageDirectory().getPath();
String[] splitPath= media_path .split("/");
final String MEDIA_PATH = "/" + splitPath[1] + "/";