相册艺术没有出现在我的RecyclerView中

时间:2018-04-30 11:00:31

标签: java android android-recyclerview cursor android-glide

我想显示内部存储空间中的歌曲列表以及专辑封面。我正在使用此代码加载列表中的歌曲。

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中显示,但没有显示专辑封面而不是我的占位符图像。没有一首歌有专辑封面。为了确保我的歌曲有与之相关的专辑封面,我使用了另一个音乐播放器应用程序,我的歌曲专辑封面显示我的代码出错了吗?

2 个答案:

答案 0 :(得分:0)

试试这个

Glide.with(context)
     .load(new File(sd.getAlbumArtUri().getPath())) // path of the album art
     .transform(new RequestOptions()
                .placeholder(R.drawable.ic_music))
     .into(holder.albumArt);

答案 1 :(得分:0)

经过大量的试验和错误后,我终于成功了。上述代码的错误如下所示

java.lang.SecurityException(External path: /storage/emulated/0/Android/data/com.android.providers.media/albumthumbs/1525023642395: Neither user 10108 nor current process has android.permission.WRITE_EXTERNAL_STORAGE.)

Uri正在将content://media/external/audio/albumart解析为外部目录/storage/emulated/0/Android/data/com.android.providers.media/albumthumbs/目录,并将专辑封面从数据库写入或缓存到设备存储,但它没有任何写入权限。 因此,只需授予写入存储权限,它就可以正常工作

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

还要求获得API&gt; = 23的许可。您还可以验证在内部存储目录中缓存的相册艺术

Android/data/com.android.providers.media/albumthumbs/