【发布时间】:2019-03-24 23:20:27
【问题描述】:
我想显示我内部存储中的歌曲列表以及专辑封面。我正在使用此代码加载列表中的歌曲。
private void getSongsList() {
final Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
final String[] cursor_cols = { MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.ALBUM_ID,
MediaStore.Audio.Media.DURATION };
final String where = MediaStore.Audio.Media.IS_MUSIC + "=1";
final Cursor cursor = getActivity().getContentResolver().query(uri,
cursor_cols, where, null, null);
if(cursor != null && cursor.moveToFirst()) {
do {
String artist = cursor.getString(cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
String album = cursor.getString(cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
String songName = cursor.getString(cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
String data = cursor.getString(cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
long albumId = cursor.getLong(cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
int duration = cursor.getInt(cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
Uri sArtworkUri = Uri
.parse("content://media/external/audio/albumart");
Uri albumArtUri = ContentUris.withAppendedId(sArtworkUri, albumId);
songsList.add(new SongData(songName, artist, data, album, albumArtUri, duration));
}while (cursor.moveToNext());
cursor.close();
Collections.sort(songsList, new Comparator<SongData>(){
public int compare(SongData a, SongData b){
return a.getSongName().compareTo(b.getSongName());
}
});
}
}
我正在使用 Glide 将图像加载到我的图像视图中
SongData sd = list.get(position);
Glide.with(context)
.load(sd.getAlbumArtUri())
.apply(new RequestOptions()
.placeholder(R.drawable.ic_music))
.into(holder.albumArt);
问题是我的所有歌曲都显示在我的 RecyclerView 中,但没有显示在专辑封面中,而是显示在我的占位符图像中。没有一首歌有专辑封面。为了确保我的歌曲有与之关联的专辑封面,我使用了另一个音乐播放器应用程序,并且我的歌曲专辑封面显示在那里我的代码有什么问题?
【问题讨论】:
标签: java android android-recyclerview cursor android-glide