如何用Picasso将歌曲专辑封面加载到ImageView?

时间:2017-12-04 00:17:31

标签: android picasso albumart

public void getSongs() {

    Uri songUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    String selection = MediaStore.Audio.Media.IS_MUSIC + "!=0";
    Cursor myCursor = getContentResolver().query(songUri, null, selection, null, null);


    if (myCursor != null && myCursor.moveToFirst()) {
        int id_Column = myCursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID);
        int data_Column = myCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
        int title_Column = myCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
        int artist_Column = myCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST);
        int album_Column = myCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID);
        do {

            Long songId = myCursor.getLong(id_Column);
            String songData = myCursor.getString(data_Column);
            String songName = myCursor.getString(title_Column);
            String songArtist = myCursor.getString(artist_Column);
            String songAlbum  = myCursor.getString(album_Column);

            long parseLong = Long.parseLong(songAlbum);
            final Uri sArtWorkUri =  Uri.parse("content://media/external/audio/albumart");

            Uri uri = ContentUris.withAppendedId(sArtWorkUri, parseLong);

            Picasso.with(this).load(uri).into(albumArt);

            QuerySongs querySongs = new QuerySongs(songId, songData, songName, songArtist);
            songList.add(querySongs);

        } while (myCursor.moveToNext());
        myCursor.close();
    }
}

如何将相册封面加载到imageView?在我的方法getSongs我用Picasso尝试过,但没有加载艺术。

我该如何解决这个问题?

谢谢, -Vince

1 个答案:

答案 0 :(得分:0)

您需要从Uri创建一个文件对象,如下所示:

...

long parseLong = Long.parseLong(songAlbum);
        final Uri sArtWorkUri =  
Uri.parse("content://media/external/audio/albumart");

Uri uri = ContentUris.withAppendedId(sArtWorkUri, parseLong);

File file = new File(uri.getPath());
Picasso.with(getContext())
        .load(file)  
        .error(R.drawable.mountain) 
        .into(albumArt);