从路径获取mp3文件

时间:2015-01-15 20:39:20

标签: java android audio-player android-music-player

我正在尝试从我的系统的文件夹路径中获取mp3文件,以便在listView中列出它,但不幸的是总是出现同样的错误。 (java.lang.NullPointerException:尝试获取null数组的长度)

class Mp3Filter implements FilenameFilter{
public boolean accept(File dir, String name){
    return (name.endsWith(".mp3"));
}
}

private static final String SD_PATH = new String(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).toString());

public void searchForSongs() {

    ListView listView;
    listView = (ListView) findViewById(R.id.listView);

    File f = new File(SD_PATH);

   try {

          if (f.listFiles(new Mp3Filter()).length > 0){
           for (File file : f.listFiles(new Mp3Filter())){
                  list.add(file.getName());
              }
       }

    }

    catch(Exception e) {
        textView2.setText(""+e);
        return;
    }

    final ArrayAdapter songList = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);
    listView.setAdapter(songList);
}

1 个答案:

答案 0 :(得分:2)

以下是您的解决方案,请使用以下代码从特定文件夹中读取MP3文件..

首先创建1个模型类,如下面的给定,列表中的GETSET文件。

<强> AudioModel.class

public class AudioModel {

    String aPath;
    String aName;
    String aAlbum;
    String aArtist;

    public String getaPath() {
        return aPath;
    }

    public void setaPath(String aPath) {
        this.aPath = aPath;
    }

    public String getaName() {
        return aName;
    }

    public void setaName(String aName) {
        this.aName = aName;
    }

    public String getaAlbum() {
        return aAlbum;
    }

    public void setaAlbum(String aAlbum) {
        this.aAlbum = aAlbum;
    }

    public String getaArtist() {
        return aArtist;
    }

    public void setaArtist(String aArtist) {
        this.aArtist = aArtist;
    }
}

现在我们有我们的模型类现在使用下面的代码来读取文件夹中的所有MP3文件。

这将返回包含音乐NAME, PATH, ARTIST, ALBUM的所有MP3文件的列表,如果您想了解更多详细信息,请参阅Media.Store.Audio doc .. https://developer.android.com/reference/android/provider/MediaStore.Audio.html

public List<AudioModel> getAllAudioFromDevice(final Context context) {

        final List<AudioModel> tempAudioList = new ArrayList<>();

        Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        String[] projection = {MediaStore.Audio.AudioColumns.DATA, MediaStore.Audio.AudioColumns.ALBUM, MediaStore.Audio.ArtistColumns.ARTIST,};
        Cursor c = context.getContentResolver().query(uri, projection, MediaStore.Audio.Media.DATA + " like ? ", new String[]{"%yourFolderName%"}, null);

        if (c != null) {
            while (c.moveToNext()) {

                AudioModel audioModel = new AudioModel();
                String path = c.getString(0);
                String album = c.getString(1);
                String artist = c.getString(2);

                String name = path.substring(path.lastIndexOf("/") + 1);

                audioModel.setaName(name);
                audioModel.setaAlbum(album);
                audioModel.setaArtist(artist);
                audioModel.setaPath(path);

                Log.e("Name :" + name, " Album :" + album);
                Log.e("Path :" + path, " Artist :" + artist);

                tempAudioList.add(audioModel);
            }
            c.close();
        }

        return tempAudioList;
    }

要读取特定文件夹的文件,请使用以下查询并在查询中写入您的文件夹名称。

Cursor c = context.getContentResolver().query(uri,
                                          projection, 
                                          MediaStore.Audio.Media.DATA + " like ? ", 
                                          new String[]{"%yourFolderName%"}, // yourFolderName 
                                          null);

如果您希望在查询..

下使用所有设备文件
Cursor c = context.getContentResolver().query(uri,
                                          projection, 
                                          null, 
                                          null, 
                                          null);

不要忘记添加存储权限..享受。