Android 设置相册缩略图

发布于 2024-09-13 12:11:40 字数 1342 浏览 1 评论 0原文

我已经检索了专辑的一些封面艺术(我有 id 和位图),现在我想将其设置到 MediaStore 中。

我尝试了很多东西:

private static final Uri ARTWORK_URI = Uri.parse("content://media/external/audio/albumart");

public static void writeArtwork(Context context, Bitmap bmp, int albumId) {
        ContentResolver res = context.getContentResolver();
        Uri uri = ContentUris.withAppendedId(ARTWORK_URI, albumId);
        LogUtil.i(TAG, "uri= " + uri);
        if (uri != null) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bmp.compress(Bitmap.CompressFormat.JPEG, 50, baos);
            byte[] bytes = baos.toByteArray();
            LogUtil.i(TAG, "Bytes: " + bytes.length);
            ContentValues values = new ContentValues();
            values.put("album_id", albumId);
            values.put("_data", bytes);
            res.insert(uri, values);
        }
    }

这给出了 java.lang.UnsupportedOperationException: Invalid URI content://media/external/audio/albumart/5

我也尝试过:

Uri artworkUri = Uri.parse("content://media/external/audio/albumart");
Uri uri = ContentUris.withAppendedId(artworkUri, album.getId());
OutputStream outStream = getContentResolver().openOutputStream(uri);
bmp.compress(Bitmap.CompressFormat.JPEG, 50, outStream);

但这给出了 FileNotFoundException。

I have retrieved some cover art for an album (I have the id and a Bitmap) and now I want to set it into the MediaStore.

I tried a bunch of stuff:

private static final Uri ARTWORK_URI = Uri.parse("content://media/external/audio/albumart");

public static void writeArtwork(Context context, Bitmap bmp, int albumId) {
        ContentResolver res = context.getContentResolver();
        Uri uri = ContentUris.withAppendedId(ARTWORK_URI, albumId);
        LogUtil.i(TAG, "uri= " + uri);
        if (uri != null) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bmp.compress(Bitmap.CompressFormat.JPEG, 50, baos);
            byte[] bytes = baos.toByteArray();
            LogUtil.i(TAG, "Bytes: " + bytes.length);
            ContentValues values = new ContentValues();
            values.put("album_id", albumId);
            values.put("_data", bytes);
            res.insert(uri, values);
        }
    }

This gives java.lang.UnsupportedOperationException: Invalid URI content://media/external/audio/albumart/5

Also I tried:

Uri artworkUri = Uri.parse("content://media/external/audio/albumart");
Uri uri = ContentUris.withAppendedId(artworkUri, album.getId());
OutputStream outStream = getContentResolver().openOutputStream(uri);
bmp.compress(Bitmap.CompressFormat.JPEG, 50, outStream);

But this gives FileNotFoundException.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文