我想知道如何使用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);
//...

最佳答案

首先,我建议您看一下:

  • Android SQLite database and content provider
  • Loaders and Loader Manager Background

  • 此代码段可能有助于您朝正确的方向前进。
    // 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();
        }
    }
    

    10-07 19:32
    查看更多