androidpicassoalbumart

Picasso not working to load album art


public void getAlbumArt(){
    final Uri albumArtUri = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;

    Cursor albumCursor = getContentResolver().query(albumArtUri, null, null, null, null);

    if (albumCursor != null && albumCursor.moveToFirst()){
        int album_Art = albumCursor.getColumnIndexOrThrow(MediaStore.Audio.Albums.ALBUM_ART);
        do {
            String albumArt = albumCursor.getString(album_Art);
            QuerySongs querySongs = new QuerySongs(null, null, null, null, null, albumArt);
            songList.add(querySongs);
        }while(albumCursor.moveToNext());
        albumCursor.close();
    }
}

Adapter class

public void onBindViewHolder(SongHolder holder, final int position) {
    //holder.tvIndex.setText(String.format(Locale.US, "%d", position));
    holder.tvSongTitle.setText(songList.get(position).getTitle());
    holder.tvArtistName.setText(songList.get(position).getArtist());

    Picasso.with(context)
            .load(Uri.parse("file://"+songList.get(position).getAlbumart()))
            .error(R.drawable.no_album)
            .into(holder.albumIv);

}

I have a problem with getting the album art from songs on the sd-card, what is the easiest way to get the album art, and display it in an imageview? I have no clue what i'm doing wrong, i looked at other posts on this forum, but with no avail, still haven't found out how to do it correctly.

Thanks,

EDIT

public void getAlbumArt(){
    final Uri albumArtUri = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;

    Cursor albumCursor = getContentResolver().query(albumArtUri, null, null, null, null);

    if (albumCursor != null && albumCursor.moveToFirst()){
        int albumart_Column = albumCursor.getColumnIndexOrThrow(MediaStore.Audio.Albums.ALBUM_ART);

        do {
            String albumArt = albumCursor.getString(albumart_Column);
            QuerySongs querySongs = new QuerySongs(null, null, null, null, null, albumArt);
            songList.add(querySongs);
        }while(albumCursor.moveToNext());
        albumCursor.close();
    }
    albumIv = findViewById(R.id.albumIv);
    Picasso.with(context)
            .load(albumArtUri)
            .error(R.drawable.no_album)
            .into(albumIv);
}

Song Constructor

package com.vince_mp3player.mp3player;

/**
* Created by Vincent on 12/11/2017.
*/

public class QuerySongs {
long id;
long albumid;
String data;
String title;
String artist;
String albumart;



   public QuerySongs(Long songId, Long albumId, String songData,  String 
   songName, String songArtist, String albumArt){
    this.id = songId;
    this.albumid = albumId;
    this.data = songData;
    this.title = songName;
    this.artist = songArtist;
    this.albumart = albumArt;

}

public Long getId(){
    return id;
}

public long getAlbumid() { return albumid; }

public String getData(){
    return data;
}


public String getTitle() {
    return title;
}


public String getArtist() {
    return artist;
}

public String getAlbumart() { return albumart; }

}

Display Song Class

public void getAlbumArt(){
    final Uri albumArtUri = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;

    Cursor albumCursor = getContentResolver().query(albumArtUri, null, null, null, null);

    if (albumCursor != null && albumCursor.moveToFirst()){
        int albumart_Column = albumCursor.getColumnIndexOrThrow(MediaStore.Audio.Albums.ALBUM_ART);

        do {
            Uri albumArt = Uri.parse(albumCursor.getString(albumart_Column));

            QuerySongs querySongs = new QuerySongs(null, null, null, null, null, albumArt);
            songList.add(querySongs);
        }while(albumCursor.moveToNext());
        albumCursor.close();
    }
}

SongAdapter

 @Override
public void onBindViewHolder(SongHolder holder, final int position) {
    //holder.tvIndex.setText(String.format(Locale.US, "%d", position));

    holder.tvSongTitle.setText(songList.get(position).getTitle());
    holder.tvArtistName.setText(songList.get(position).getArtist());

    Picasso.with(context)
            .load(Uri.parse(songList.get(songIndex).getAlbumart()))
            .error(R.drawable.no_album)
            .into(holder.albumIv);

}

EDIT 2

 public void getSongs() {
    final 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 albumId_Column = myCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_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);

        do {

            Long songId = myCursor.getLong(id_Column);
            Long albumId = myCursor.getLong(albumId_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);

            Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
            Uri albumArtUri = ContentUris.withAppendedId(sArtworkUri, albumId);

            Bitmap bitmap = null;

            try {
                bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), albumArtUri);
                bitmap = Bitmap.createScaledBitmap(bitmap, 30, 30, true);
            }catch (FileNotFoundException exception ){
                exception.printStackTrace();
                bitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.no_album);
            }catch (Exception e){
                e.printStackTrace();
            }

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

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

Solution

  • First, you're going to need runtime permissions to read files from disk.

    Try doing this in the cursor loop

    Uri albumArt = Uri.parse(albumCursor.getString(album_Art));
    

    Add that to your list (after you change it's type from a string)

    private Uri albumart;
    public Uri getAlbumart() { return albumart; }
    

    Picasso loads Uri objects, and I'm not sure file:// is resolvable, or appending it to the album art path is correct.

    public void onBindViewHolder(SongHolder holder, final int position) {
    
        holder.tvSongTitle.setText(songList.get(position).getTitle());
        holder.tvArtistName.setText(songList.get(position).getArtist());
    
        Picasso.with(context)
                .load(songList.get(position).getAlbumart()) 
                .error(R.drawable.no_album)
                .into(holder.albumIv);
    
    }
    

    Also, lots of code here, but might get something out of it - Get Album Art With Album Name Android