问题描述
我希望能够获得当前正在播放的歌曲的专辑封面.我可以使用
I want to be able to get the album art of the song that is currently playing. I am able to get the name, album, artist with the use of
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();
但是,这对获取专辑封面没有帮助.此处要求获取专辑插图的大多数帖子都呼吁使用
However, this does not help me with getting album art. Most of the posts on here that ask for getting album art call for the use of
但是我似乎无法弄清楚它是如何工作的.当我尝试使用
But I can't seem to figure out how to work it. When I tried to use
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
}
(来自此处)或与此类似的任何内容,我一直遇到没有AlbumId的问题.我读到我应该将专辑ID设置为
(From here ) or anything similar to this I keep running into the problem of not having a albumId. I read that I should set albumid to
相册ID = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID))
但是如果没有另一个光标,我似乎无法获得Long,如果不使用预先存在的白蛋白,我将无法弄清楚该怎么做.任何帮助将不胜感激.
but I can't seem to get a Long without having another cursor which I can't figure out how to do to without the use of a pre-existing albumid. Any help would be greatly appreciated.
推荐答案
获取当前播放歌曲的专辑ID并调用以下函数以获取专辑封面:
Get Album ID of current playing song and call following function to get Album Art :
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;
}
这篇关于如何获得当前正在播放的歌曲的专辑封面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!