我希望能够获得当前正在播放的歌曲的专辑封面。我可以使用
获取名称,专辑,艺术家 private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
String cmd = intent.getStringExtra("command");
Log.v("tag ", action + " / " + cmd);
String artist = intent.getStringExtra("artist");
String album = intent.getStringExtra("album");
String track = intent.getStringExtra("track");
Log.v("tag", artist + ":" + album + ":" + track);
Fullname = (artist + ":" + album + ":" + track);
Toast.makeText(MusicPlayer.this, track, Toast.LENGTH_SHORT).show();
update();
但是,这对于获取专辑封面并没有帮助。这里要求获得专辑封面的大多数帖子都要求使用
MediaStore.Audio.AlbumColumns.AlbumArt
但我似乎无法弄清楚如何使用它。当我尝试使用
时Cursor cursor = getActivity().managedQuery(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
new String[] {MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ALBUM_ART},
MediaStore.Audio.Albums._ID+ "=?",
new String[] {String.valueOf(albumId)},
null);
if (cursor.moveToFirst()) {
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
// do whatever you need to do
}
(来自here)或类似的任何内容我一直遇到没有albumId的问题。我读到我应该将albumid设置为
album_id = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID))
但是如果不使用预先存在的专辑,我无法在没有另一个光标的情况下获得很长时间,而我无法理解如何做到这一点。任何帮助将不胜感激。
答案 0 :(得分:0)
我遇到了同样的问题。意图只有媒体ID。所以我们必须得到与该媒体文件相关的专辑ID。
这是我使用的解决方案:
//get the song's id from intent
long songId = intent.getLongExtra("id", -1);
//get the albumid using media/song id
if(songId!=-1) {
String selection = MediaStore.Audio.Media._ID + " = "+songId+"";
Cursor cursor = getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, new String[] {
MediaStore.Audio.Media._ID, MediaStore.Audio.Media.ALBUM_ID},
selection, null, null);
if (cursor.moveToFirst()) {
long albumId = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
Log.d("Album ID : ", ""+albumId);
Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
Uri albumArtUri = ContentUris.withAppendedId(sArtworkUri, albumId);
//set the album art in imageview
albumArt.setImageURI(albumArtUri);
}
cursor.close();
}
答案 1 :(得分:0)
获取当前播放歌曲的专辑ID并调用以下功能以获取专辑封面:
public static Bitmap getAlbumart(Context context,Long album_id){
Bitmap bm = null;
BitmapFactory.Options options = new BitmapFactory.Options();
try{
final Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "r");
if (pfd != null){
FileDescriptor fd = pfd.getFileDescriptor();
bm = BitmapFactory.decodeFileDescriptor(fd, null, options);
pfd = null;
fd = null;
}
} catch(Error ee){}
catch (Exception e) {}
return bm;
}