我为相册检索了一些封面图片(我有id和Bitmap),现在我想将它设置为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:无效的URI内容:// 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。