如何以编程方式获取音乐专辑封面

Posted

技术标签:

【中文标题】如何以编程方式获取音乐专辑封面【英文标题】:how to get music album art cover programmatically 【发布时间】:2021-04-28 03:47:05 【问题描述】:

我想获取音乐专辑封面以在 gridview 的 imageview 中显示它们。 这是我的代码

void loadAudioData() 
        ContentResolver contentResolver = Objects.requireNonNull(getActivity()).getContentResolver();
        final Uri uri = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;
        final String albumId = MediaStore.Audio.Albums._ID;
        final String albumName = MediaStore.Audio.Albums.ALBUM;
        final String artist = MediaStore.Audio.Albums.ARTIST;
        final String albumArt = MediaStore.Audio.AlbumColumns.ALBUM_ART;
        final String album_id = MediaStore.Audio.AlbumColumns.ALBUM_ID;
        final Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
        String[] projection = albumId, albumName, artist, albumArt, album_id;
        Cursor cursor = contentResolver.query(uri, projection, null, null, null);

        Bitmap bitmap = null;
        if (cursor != null && cursor.moveToFirst())
            while (cursor.moveToNext()) 
                long album_idx = cursor.getLong(cursor.getColumnIndexOrThrow(album_id));
                Uri albumArtUri = ContentUris.withAppendedId(sArtworkUri, album_idx);
                try 
                    bitmap = MediaStore.Images.Media.getBitmap(getContext().getContentResolver(), albumArtUri);
                 catch (IOException e) 
                    Log.e("bitmap = null", "bitmap = null");
                
                if (bitmap == null)
                    songsList.add(new Songs(R.drawable.album_cover, cursor.getString(cursor.getColumnIndex(albumName)),
                            cursor.getString(cursor.getColumnIndex(artist))));
                else
                    songsList.add(new Songs(bitmap, cursor.getString(cursor.getColumnIndex(albumName)),
                            cursor.getString(cursor.getColumnIndex(artist))));
            
        cursor.close();
    

问题是位图始终为空,else 语句始终执行,尽管有专辑的封面图像并且它们显示在其他已安装的应用程序中。 我尝试了太多解决方案,但都没有帮助到最后

【问题讨论】:

【参考方案1】:

尝试使用 FileDescriptor 获取位图。查看更新的代码:

while (cursor.moveToNext()) 
                long album_idx = cursor.getLong(cursor.getColumnIndexOrThrow(album_id));
                Uri albumArtUri = ContentUris.withAppendedId(sArtworkUri, album_idx);
                try 
                    ParcelFileDescriptor pfd = getContext().getContentResolver().openFileDescriptor(uri, "r");
                    if (pfd != null) 
                        FileDescriptor fd = pfd.fileDescriptor;
                        bitmap = BitmapFactory.decodeFileDescriptor(fd, null,options);
                        pfd.close();
                        fd = null;
                    
                catch (IOException e) 
                    Log.e("bitmap = null", "bitmap = null");
                
                if (bitmap == null)
                    songsList.add(new Songs(R.drawable.album_cover, cursor.getString(cursor.getColumnIndex(albumName)),
                            cursor.getString(cursor.getColumnIndex(artist))));
                else
                    songsList.add(new Songs(bitmap, cursor.getString(cursor.getColumnIndex(albumName)),
                            cursor.getString(cursor.getColumnIndex(artist))));
            

【讨论】:

不幸的是同样的结果

以上是关于如何以编程方式获取音乐专辑封面的主要内容,如果未能解决你的问题,请参考以下文章

从音频文件中获取专辑封面、歌曲名称、专辑名称和艺术家姓名

Facebook iOS SDK:获取专辑封面图片

如何在 Win8 C# 应用程序中设置专辑封面?

从 mp3 文件中获取专辑封面?

如何从 MP3 ID3 标签中提取专辑封面?

从封面艺术档案 (archive.org) API 中获取专辑封面会由于重定向导致 CORS 错误