【问题标题】: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 语句始终执行,尽管有专辑的封面图像并且它们显示在其他已安装的应用程序中。 我尝试了太多解决方案,但都没有帮助到最后

【问题讨论】:

    标签: java android android-music-player


    【解决方案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))));
                }
    

    【讨论】:

    • 不幸的是同样的结果
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-02
    • 1970-01-01
    • 2021-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多