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();
}
}
最佳答案
首先,您将需要运行时权限才能从磁盘读取文件。
尝试在游标循环中执行此操作
Uri albumArt = Uri.parse(albumCursor.getString(album_Art));
将其添加到列表中(从字符串更改其类型后)
private Uri albumart;
public Uri getAlbumart() { return albumart; }
毕加索会加载
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
关于android - picasso 无法加载专辑封面,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47685449/