使用专辑封面填充歌曲列表 - 无法获得专辑封面

Populating a list of songs with Album Art - Not able to get Album art

我目前正在为设备上的音乐制作音乐 browser/player。我成功地用歌曲名称和他们的艺术家填充了列表视图,但我无法显示与歌曲相关的专辑封面。我不太确定获取专辑封面的正确途径是什么以及如何将其与歌曲相关联。代码构建但不显示专辑封面。我猜是 albumArtUri 不好。你能帮我找到正确的专辑封面 Uri 吗?

感谢您的帮助,不胜感激:)

朱利安

这是我的 SongAdapter 代码:

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

感谢这个 Whosebug 答案,我找到了答案:Getting album art from the audio file Uri