将位图图像保存到画廊 android 10 的特定位置 [英] Save bitmap image to specific location of gallery android 10
本文介绍了将位图图像保存到画廊 android 10 的特定位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用此代码:
MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "title", "description");
而且运行良好.
问题:
- 它会自动在图库中创建一个名为图片"的文件夹.但我想要不同的名称,例如我的应用名称.
- MediaStore 的insertImage() 函数在Android 10 中折旧:
<块引用>
public static String insertImage (ContentResolver cr,字符串图像路径,字符串名称,字符串描述)
<块引用>
此方法已在 API 级别 29 中弃用.插入图像应使用 MediaColumns#IS_PENDING 执行,它提供了对生命周期的更丰富的控制.
我已经阅读了文档,但实际上并不了解 IS_PENDING 以及如何使用它.
解决方案
试试这个
private void saveImage(Bitmap bitmap, @NonNull String name) throws IOException {布尔值保存;输出流 fos;如果 (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {ContentResolver 解析器 = 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);} 别的 {String imagesDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString() + File.separator + IMAGES_FOLDER_NAME;File file = new File(imagesDir);如果 (!file.exists()) {文件.mkdir();}File image = new File(imagesDir, name + ".png");fos = 新文件输出流(图像)}保存 = bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);fos.flush();fos.close();}
I am using this code:
MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "title" , "description");
and it is working well.
Problems:
- It is creating a folder called "Pictures" in gallery automatically. But I want different name, for example my app's name.
- insertImage() function of MediaStore is depreciated in android 10:
public static String insertImage (ContentResolver cr, String imagePath, String name, String description)
This method was deprecated in API level 29. inserting of images should be performed using MediaColumns#IS_PENDING, which offers richer control over lifecycle.
I have read the documentation and don't actually understand IS_PENDING and how to use it.
解决方案
Try this
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();
}
这篇关于将位图图像保存到画廊 android 10 的特定位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文