毕加索没有加载专辑封面

时间:2017-12-06 23:57:18

标签: android picasso albumart

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

适配器类

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

}

我在使用SD卡上的歌曲获取专辑封面时遇到问题,获取专辑封面的最简单方法是什么,并将其显示在imageview中? 我不知道我做错了什么,我看了这个论坛上的其他帖子,但没有用,仍然没有找到如何正确地做到这一点。

谢谢,

修改

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

歌曲构造器

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

}

显示歌曲类

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

}

编辑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();
    }
}

2 个答案:

答案 0 :(得分:1)

获取相册ID

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

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

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

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

在适配器类

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


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

    Picasso.with(context)
            .load(albumArtUri)
            .error(R.drawable.no_album)
            .into(holder.albumIv);
}

答案 1 :(得分:0)

首先,您需要运行时权限才能从磁盘读取文件。

尝试在游标循环中执行此操作

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

将其添加到列表中(从字符串中更改其类型后)

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

Picasso加载Uri个对象,我不确定file://是否可解析,或者将其附加到专辑封面上是正确的。

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

}

此外,这里有很多代码,但可能会从中获得一些东西 - Get Album Art With Album Name Android