我正在使用以下代码:
MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "title" , "description");而且效果很好。
问题:
公共静态字符串insertImage (ContentResolver cr、String imagePath、String name、String description)该方法在API级别29中被废弃。图像的插入应该使用MediaColumns#IS_PENDING来执行,它提供了更丰富的生命周期控制。
我读过这些文档,并不真正理解IS_PENDING以及如何使用它。
发布于 2020-09-07 11:56:10
试试这个代码:-
private void saveImage(Bitmap bitmap, @NonNull String name) throws IOException {
boolean saved;
OutputStream fos;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ContentResolver resolver = mContext.getContentResolver();
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, name);
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/png");
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, "DCIM/" + IMAGES_FOLDER_NAME);
Uri imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
fos = resolver.openOutputStream(imageUri);
} else {
String imagesDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DCIM).toString() + File.separator + IMAGES_FOLDER_NAME;
File file = new File(imagesDir);
if (!file.exists()) {
file.mkdir();
}
File image = new File(imagesDir, name + ".png");
fos = new FileOutputStream(image);
}
saved = bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
}https://stackoverflow.com/questions/63776744
复制相似问题