我目前正在设备上构建音乐浏览器/音乐播放器。我成功地用歌曲的名称及其艺术家填充了列表视图,但我无法显示与歌曲相关的专辑封面。我不太确定获得专辑封面的正确途径是什么,以及如何将其与歌曲相关联。显示代码生成,但不显示专辑封面。我猜专辑ArtUri不好。你能帮我找到正确的专辑封面乌里吗?
感谢您的帮助,非常感谢:)
朱利安
这是我的歌曲适配器代码:
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
using Android.Provider;
using Android.Database;
using Android.Net;
namespace project
{
public class SongsAdapter:CursorAdapter
{
List<Song> songsList;
Activity context;
public SongsAdapter (Activity context, ICursor cursor, List<Song> theSongs): base(context,cursor)
{
this.context = context;
songsList = theSongs;
}
public override void BindView(View view, Context context, ICursor cursor)
{
var song_title = view.FindViewById<TextView> (Resource.Id.song_title);
var song_artist = view.FindViewById<TextView> (Resource.Id.song_artist);
var song_album_art = view.FindViewById<ImageView> (Resource.Id.song_album_art);
song_title.Text = songsList [cursor.Position].Title;
song_artist.Text = songsList [cursor.Position].Artist;
//If there is an image to display, it will display it. If not, it will use a default image
if (songsList [cursor.Position].AlbumId == null) {
song_album_art = view.FindViewById<ImageView> (Resource.Id.song_album_art);
song_album_art.SetImageResource (Resource.Drawable.ic_action_user); //Image needs to be set
} else {
var songUri = ContentUris.WithAppendedId (MediaStore.Audio.Media.ExternalContentUri, songsList [cursor.Position].Id);
var albumArtUri = Android.Net.Uri.WithAppendedPath(songUri,MediaStore.Audio.Albums.EntryContentType);
song_album_art.SetImageURI (albumArtUri);
}
}
public override View NewView(Context context, ICursor cursor, ViewGroup parent)
{
return this.context.LayoutInflater.Inflate(Resource.Layout.songslistitem, parent, false);
}
}
}
以下是我如何称呼SongAdapter:
ListView listView;
view = LayoutInflater.From (container.Context).Inflate (Resource.Layout.songlist, container, false);
listView = view.FindViewById<ListView> (Resource.Id.List);
List<Song> songsList;
var uri = MediaStore.Audio.Media.ExternalContentUri;
string[] projection = {
MediaStore.Audio.Media.InterfaceConsts.Id,
MediaStore.Audio.Media.InterfaceConsts.AlbumId,
MediaStore.Audio.Media.InterfaceConsts.Title,
MediaStore.Audio.Media.InterfaceConsts.Artist,
};
var loader = new CursorLoader (container.Context, uri, projection, null, null, null);
var cursor = (ICursor)loader.LoadInBackground ();
songsList = new List<Song> ();
if (cursor.MoveToFirst ()) {
do {
songsList.Add (new Song {
Id = cursor.GetLong (cursor.GetColumnIndex (projection [0])),
AlbumId = cursor.GetString (cursor.GetColumnIndex (projection [1])),
Title = cursor.GetString (cursor.GetColumnIndex (projection [2])),
Artist = cursor.GetString (cursor.GetColumnIndex (projection [3]))
});
} while (cursor.MoveToNext ());
}
listView.Adapter = new SongsAdapter (context,cursor,songsList);
我能够通过在歌曲适配器中执行此操作来解决我的问题:
long album_id = cursor.GetLong(cursor.GetColumnIndex(MediaStore.Audio.Albums.InterfaceConsts.AlbumId));
var songCover = Android.Net.Uri.Parse ("content://media/external/audio/albumart");
var songAlbumArtUri = ContentUris.WithAppendedId (songCover, album_id);
song_artist.Text = songAlbumArtUri.ToString();
song_album_art.SetImageURI (songAlbumArtUri);
多亏了这个堆栈溢出答案,我找到了答案:从音频文件 Uri 获取专辑封面