我目前正在该设备上构建音乐的音乐浏览器/播放器。我已经成功地在列表视图中填充了歌曲及其艺术家的名字,但是我无法显示与该歌曲相关的albumart。我不太确定获得专辑封面的正确途径是什么?还有如何将它与歌曲相关联。代码构建但不显示专辑封面。我猜可能是 albumArtUri 并不好。你能帮我找到合适的albumart 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);