音乐要存在SD卡中哦,不然要写存储地址
先来一个读写的SD卡权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
然后就是
list = new ArrayList<Music_Data>();
ContentResolver resolver = getContentResolver();
Cursor que = resolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
if(que!=null){
que.moveToFirst();
que.moveToPrevious();
while(que.moveToNext()){
// 歌曲ID:MediaStore.Audio.Media._ID
int int_ID = que.getInt(que.getColumnIndexOrThrow(MediaStore.Audio.Media._ID));
// 歌曲的名称 :MediaStore.Audio.Media.TITLE
String tilte = que.getString(que.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
// 歌曲的专辑名:MediaStore.Audio.Media.ALBUM
String album = que.getString(que.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
// 歌曲文件的大小 :MediaStore.Audio.Media.SIZE
long size = que.getLong(que.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE));
// 歌曲的歌手名: MediaStore.Audio.Media.ARTIST
String artist = que.getString(que.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
// 歌曲文件的路径 :MediaStore.Audio.Media.DATA
String url = que.getString(que.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
// 歌曲的总播放时长 :MediaStore.Audio.Media.DURATION
int duration = que.getInt(que.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
Music_Data music_Data = new Music_Data(int_ID, tilte, album, size, artist, url, duration);
list.add(music_Data);
Log.e("tag", list+"");
}
}
}
再建一个音乐类
package com.example.contentprovider_music;
import android.provider.MediaStore;
public class Music_Data {
// 歌曲ID:MediaStore.Audio.Media._ID
int int_ID;
// 歌曲的名称 :MediaStore.Audio.Media.TITLE
String tilte ;
// 歌曲的专辑名:MediaStore.Audio.Media.ALBUM
String album;
// 歌曲文件的大小 :MediaStore.Audio.Media.SIZE
long size;
// 歌曲的歌手名: MediaStore.Audio.Media.ARTIST
String artist;
// 歌曲文件的路径 :MediaStore.Audio.Media.DATA
String url ;
// 歌曲的总播放时长 :MediaStore.Audio.Media.DURATION
int duration ;
public Music_Data(int int_ID, String tilte, String album, long size,
String artist, String url, int duration) {
super();
this.int_ID = int_ID;
this.tilte = tilte;
this.album = album;
this.size = size;
this.artist = artist;
this.url = url;
this.duration = duration;
}
@Override
public String toString() {
return "Music_Data [int_ID=" + int_ID + ", tilte=" + tilte + ", album="
+ album + ", size=" + size + ", artist=" + artist + ", url=" + url
+ ", duration=" + duration + "]";
}
}