如何将所有音乐文件导入我的应用程序?

时间:2014-04-08 06:14:09

标签: java android xml android-sdcard

所以我听说过方法MediaStore,但是我发现创建一些可以帮助我从整个设备中检索音乐文件而不仅仅是单个文件夹的代码真的很难和令人困惑。我已经尝试过检查很多源代码,但它们都是如此长而令人困惑。我想知道从整个设备中检索音乐的好方法。

这里相关问题的许多答案并没有真正解释该怎么做。它也是。一个链接或“试试这个”,我发现它不清楚,很难理解。

我的SongsManager.Java代码 -

public class SongsManager {
    // SDCard Path
    final String MEDIA_PATH = ("/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())!=null && 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;
    }

    /*
     * The following class filters all files that have an extension of .mp3||.MP3
     */
    class FileExtensionFilter implements FilenameFilter {
        public boolean accept(File dir, String name) {
            return (name.endsWith(".mp3") || name.endsWith(".MP3"));
        }
    }

好吧,我找到了一种使用此代码的方法 -

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() {

}

/**
 * Function to read all mp3 files and store the details in
 * ArrayList
 * */
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);
    }
}

1 个答案:

答案 0 :(得分:0)

这将帮助您获取设备中的所有媒体文件。所有最佳:)

 String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
    //your projection statement 
    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,
            MediaStore.Audio.Media.ALBUM_ID
    };
    //query 
    cursor = this.managedQuery(
            MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            projection,
            selection,
            null,
            null);


    while(cursor.moveToNext()){
            songs.add(cursor.getString(0));
            songs.add(cursor.getString(1));
            songs.add(cursor.getString(2));
            songs.add(cursor.getString(3));
            songs.add(cursor.getString(4));
            songs.add(cursor.getString(5));
            album_id.add((long) cursor.getFloat(6));
    } 
    int a[]= new int[]{R.id.textView1 ,R.id.textView3};//, R.id.textview2};
    ListAdapter adapter = new SimpleCursorAdapter(this,
            R.layout.items, cursor, new String[]{MediaStore.Audio.Media.TITLE,           MediaStore.Audio.Media.ARTIST/*, MediaStore.Audio.Media.DURATION*/} ,a);
            setListAdapter(adapter); 
}