Android set Album Thumbnail

ε祈祈猫儿з 提交于 2019-12-12 07:40:38

问题


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.

来源:https://stackoverflow.com/questions/3450076/android-set-album-thumbnail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!