毕加索无法加载专辑封面
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();
}
}
适配器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);
}
我在从 SD 卡上的歌曲获取专辑封面时遇到问题,获取专辑封面并将其显示在图像视图中的最简单方法是什么?
我不知道我做错了什么,我看了这个论坛上的其他帖子,但没有用,仍然没有找到正确的方法。
谢谢,
编辑
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; }
}
显示歌曲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();
}
}
歌曲适配器
@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; }
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
获取专辑 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();
}
}
在你的适配器中Class
@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);
}
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();
}
}
适配器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);
}
我在从 SD 卡上的歌曲获取专辑封面时遇到问题,获取专辑封面并将其显示在图像视图中的最简单方法是什么? 我不知道我做错了什么,我看了这个论坛上的其他帖子,但没有用,仍然没有找到正确的方法。
谢谢,
编辑
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; }
}
显示歌曲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();
}
}
歌曲适配器
@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; }
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
获取专辑 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();
}
}
在你的适配器中Class
@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);
}