How can i load song album art to an ImageView with Picasso?

663 Views Asked by At
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();
    }
}

How can i load album covers to an imageView? In my method getSongs i tried it with Picasso, but doesn't load the art.

How can i solve this?

Thanks, -Vince

1

There are 1 best solutions below

7
On

You need to create a file object from the Uri as below:

...

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);