如何使用getExternalStorageDirectory()从内部存储加载多首歌曲

时间:2018-06-05 11:06:09

标签: java android android-mediaplayer

我正在开发自定义媒体播放器应用程序,该应用程序必须从SD卡和内部存储器加载歌曲。我到处搜索找到一种从内部存储加载歌曲的方法,我检查的大多数帖子只是使用localStorage来加载单个MP3文件而不是多个。我可以从SD卡(存储卡)加载所有歌曲,但无法从内部存储加载。有没有办法加载存储在内部存储器中的所有歌曲?

我的代码,

Environment.getExternalStorageDirectory()

在这里你可以看到我正在使用 private void loadSongs() { Uri uri = MediaStore.Audio.Media.INTERNAL_CONTENT_URI; Uri uriPath; String MEDIA_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/"; String selection = MediaStore.Audio.Media.IS_MUSIC + "!=0"; Cursor cursor = getContentResolver().query(uri, null, selection, null, null); if (cursor != null) { if (cursor.moveToFirst()) { do { String name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)); String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST)); String url = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA)); Log.i("DATA111", MEDIA_PATH); mediaMetadataRetrieve = new MediaMetadataRetriever(); mediaMetadataRetrieve.setDataSource(MEDIA_PATH + "/Music/"); art = mediaMetadataRetrieve.getEmbeddedPicture(); if (art != null) { songImage = BitmapFactory.decodeByteArray(art, 0, art.length); } SongInfo s = new SongInfo(name, artist, url, songImage); _songs.add(s); } while (cursor.moveToNext()); } cursor.close(); songAdapter = new SongAdapter(MainActivity.this, _songs); } } 这是一个String类型变量,其中MEDIA_PATH用于加载多首歌曲,但我得到的只是Environment.getExternalStorageDirectory()错误(是的,我可以小心翼翼地看到我得到IllegalArgumentException这意味着路径出了问题,我试图解决它,但我无法这样做。)

有人能告诉我在自定义媒体播放器中加载内部存储中的所有歌曲的方法吗?顺便说一句,如果我错了,请纠正我

1 个答案:

答案 0 :(得分:0)

public void getSongs(Context context, String location){
    Uri songUri = ((location == "internal")?
            MediaStore.Audio.Media.INTERNAL_CONTENT_URI:
            MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);

    //Query devices for files (This class provides applications access to the content model.)
    ContentResolver contentResolver = context.getContentResolver();

    //This interface provides random read-write access to the result set returned by a database query.
    Cursor myCursor;


    //This is the information i want to get from all the songs on my device.
    //Columns for audio file that show up in multiple tables.
    String SONG_ID = MediaStore.Audio.Media._ID;
    String SONG_DATA = MediaStore.Audio.Media.DATA;
    String SONG_TITLE =  MediaStore.Audio.Media.DISPLAY_NAME;
    String SONG_ARTIST = MediaStore.Audio.Media.ARTIST;

    //This is what i'll retrieve from the song table.
    String[] songColumns = {
            SONG_ID,
            SONG_DATA,
            SONG_TITLE,
            SONG_ARTIST,
    };


    //Show music files only (IS_MUSIC returns Non-zero if the audio file is music).
    final String musicOnly = MediaStore.Audio.Media.IS_MUSIC+"=1";

    myCursor = contentResolver.query(songUri, songColumns, musicOnly, null, null);

    if (myCursor != null && myCursor.moveToFirst()) {
        int songId = myCursor.getInt(myCursor.getColumnIndexOrThrow(SONG_ID));
        String songData = myCursor.getString(myCursor.getColumnIndexOrThrow(SONG_DATA));
        String songTitle = myCursor.getString(myCursor.getColumnIndexOrThrow(SONG_TITLE));
        String songArtist = myCursor.getString(myCursor.getColumnIndexOrThrow(SONG_ARTIST));

        do {

            Song song = new Song(songId, songData);

            song.setTitle(songTitle);
            song.setArtist(songArtist);


            //Add the song to the global ArrayList 'songList'.
            songList.add(song);

        } while (myCursor.moveToNext());
    }else{
        //What to do if no songs are found?
    }
    myCursor.close();

歌曲课程

public class Song implements Serializable {

  private long id;
  private String data;
  private String title = "";
  private String artist = "";


public Song(long songId, String songData){
    this.id = songId;
    this.data = songData;

}

public long getId(){
    return id;
}

public String getData(){return data;}

//Optional meta data

public void setTitle(String title){
    this.title = title;
}

public String getTitle() {
    return title;
}

public void setArtist(String artist){
    this.artist = artist;
}

public String getArtist() {
    return artist;
}