如何展示专辑封面?

时间:2013-11-20 06:52:45

标签: android

我想知道如何使用android.provider.MediaStore.Audio.Albums.ALBUM.Album_Art显示专辑的专辑封面。

我使用以下代码从路径中提取元数据,该代码适用于歌曲,但我只是不知道如何为专辑/艺术家显示专辑封面。

MediaMetadataRetriever mmr = new MediaMetadataRetriever();
byte[] rawArt = null;
float ht_px = TypedValue.applyDimension(
        TypedValue.COMPLEX_UNIT_DIP, 200, getResources().getDisplayMetrics());
float wt_px = TypedValue.applyDimension(
        TypedValue.COMPLEX_UNIT_DIP, 200, getResources().getDisplayMetrics());
BitmapFactory.Options bfo=new BitmapFactory.Options();
try {
    mmr.setDataSource(songdetails.get(swapnumber).Path);
    StackBlurManager _stackBlurManager;
    rawArt = mmr.getEmbeddedPicture();
    if ( rawArt != null)  { 
        bitmap2 = BitmapFactory.decodeByteArray(rawArt, 0, rawArt.length, bfo);
        bitmap3 = Bitmap.createScaledBitmap(bitmap2, (int) ht_px, (int) wt_px, true);
//...

1 个答案:

答案 0 :(得分:3)

首先,我建议您查看:

此代码段可能有助于您朝着正确的方向前进。

// Query URI
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

// Columns
String[] select = {
    MediaStore.Audio.Media._ID,
    MediaStore.Audio.Media.ARTIST,
    MediaStore.Audio.Media.ALBUM,
    MediaStore.Audio.Media.TITLE,
    MediaStore.Audio.Media.DATA,
    MediaStore.Audio.Media.ALBUM_ID,
    MediaStore.Audio.Media.DURATION
};

// Where
String where = MediaStore.Audio.Media.IS_MUSIC + "=1";

// Perform the query
Cursor cursor = context.getContentResolver().query(uri, cursor_cols, where, null, null);

if (cursor.moveToFirst()) {
    while (!cursor.isAfterLast()) {
        long albumId = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
        String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
        String album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
        String track = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
        String data = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
        int duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));

        final Uri ART_CONTENT_URI = Uri.parse("content://media/external/audio/albumart");
        Uri albumArtUri = ContentUris.withAppendedId(ART_CONTENT_URI, albumId);

        Bitmap bitmap = null;
        try {
            bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), albumArtUri);
        } catch (Exception exception) {
            // log error
        }

        cursor.moveToNext();
    }
}