此方法返回带有歌曲路径和专辑封面的 ArrayList。
public static ArrayList<CommonModel> getAllMusicPathList(Context context) {
ArrayList<CommonModel> musicPathArrList = new ArrayList<>();
Uri songUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor cursorAudio = context.getContentResolver().query(songUri, null, null, null, null);
if (cursorAudio != null && cursorAudio.moveToFirst()) {
Cursor cursorAlbum;
if (cursorAudio != null && cursorAudio.moveToFirst()) {
do {
Long albumId = Long.valueOf(cursorAudio.getString(cursorAudio.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID)));
cursorAlbum = context.getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ALBUM_ART},
MediaStore.Audio.Albums._ID + "=" + albumId, null, null);
if(cursorAlbum != null && cursorAlbum.moveToFirst()){
String albumCoverPath = cursorAlbum.getString(cursorAlbum.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
String data = cursorAudio.getString(cursorAudio.getColumnIndex(MediaStore.Audio.Media.DATA));
musicPathArrList.add(new CommonModel(data,albumCoverPath ,false));
}
} while (cursorAudio.moveToNext());
}
}
return musicPathArrList;
}
这是 CommonModel 。
public class CommonModel {
private String path;
private boolean selected;
public String getAlbumCoverPath() {
return albumCoverPath;
}
public void setAlbumCoverPath(String albumCoverPath) {
this.albumCoverPath = albumCoverPath;
}
private String albumCoverPath;
public CommonModel(String path, String albumCoverPath, boolean b) {
this.path = path;
this.albumCoverPath=albumCoverPath;
this.selected=b;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public boolean getSelected() {
return selected;
}
public void setSelected(boolean selected) {
selected = selected;
}
}