android - 如何将图像保存到 Android Q 中的相机文件夹?

标签 android android-10.0

我需要将图像保存到相机文件夹,但由于 Android Q getExternalStoragePublicDirectory 已弃用,我以另一种方式进行。 我有什么(此方法接收位图及其名称):

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);
        OutputStream fos = resolver.openOutputStream(imageUri);
        saved = bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.flush();
        fos.close();
    } 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"
        );

        final long fileHashCode = image.hashCode();
        Logger.d(TAG, "saveImage, saving image file, hashCode = " + fileHashCode);

        FileOutputStream fos = new FileOutputStream(image);
        saved = bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.flush();
        fos.close();
    }

这完全适用于所有需要的操作系统版本,但它看起来不准确,我想找到一种更通用的方法。 我尝试使用内容值或尝试与 Q 类似的方法,但它不起作用。我在这里看到了很多问题,但它们都不能帮助我。

问题是如何为低于 Q 的操作系统优化保存?

最佳答案

我能写的最通用的版本是:

private Uri saveImage(Context context, Bitmap bitmap, @NonNull String folderName, @NonNull String fileName) throws IOException {
    OutputStream fos = null;
    File imageFile = null;
    Uri imageUri = null;

    try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            ContentResolver resolver = context.getContentResolver();
            ContentValues contentValues = new ContentValues();
            contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, fileName);
            contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/png");
            contentValues.put(
                    MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES + File.separator + folderName);
            imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);

            if (imageUri == null)
                throw new IOException("Failed to create new MediaStore record.");

            fos = resolver.openOutputStream(imageUri);
        } else {
            File imagesDir = new File(Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_PICTURES).toString() + File.separator + folderName);

            if (!imagesDir.exists())
                imagesDir.mkdir();

            imageFile = new File(imagesDir, fileName + ".png");
            fos = new FileOutputStream(imageFile);
        }


        if (!bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos))
            throw new IOException("Failed to save bitmap.");
        fos.flush();
    } finally {
        if (fos != null)
            fos.close();
    }
    
    if (imageFile != null) {//pre Q
        MediaScannerConnection.scanFile(context, new String[]{imageFile.toString()}, null, null);
        imageUri = Uri.fromFile(imageFile);
    }
    return imageUri;
}

如果您找到更好的方法,请在此处发布,我会将其标记为答案。

关于android - 如何将图像保存到 Android Q 中的相机文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57357175/

相关文章:

android - 播放 mp3 Android 异步。播放但随后崩溃线程

c# - Android 4.4 - 发送短信一般失败 - 服务蓝牙链接

java - 使用 OpenVPN 创建 VPN

android - CallScreeningService - 几秒钟后断开调用 - Android 10

android - Android中如何设置图片的背景颜色

android - E/广告 : Fail to get isAdIdFakeForDebugLogging with error code 3

java - Android 中的 Getter 和 Setter 返回 null

linux - 如何从 AOSP 为 Android 构建 Linux 内核?

java - boot_completed 不适用于 Android 10 Q API 级别 29

Android 10 如何在插入媒体时按当前时间更改 date_taken?