首页 > 解决方案 > 使用Android 29下的Camera X ImageCapture.OutputFileOptions.Builder将图像保存到特定文件夹

问题描述

我正在尝试实现 Camera X 应用程序。我现在面临的问题是我无法弄清楚如何将下面的图像保存到特定文件夹中ImageCapture.OutputFileOptions.Builder

我当前的代码如下。我需要采用其他方式吗?或者我也可以这样做?


    private void capturePhoto() {

        showProgress(true);

        long currentTime = System.currentTimeMillis();

        ContentValues contentValues = new ContentValues();

        contentValues.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");

        contentValues.put(MediaStore.Images.Media.DISPLAY_NAME, makeImageName(currentTime));

        if (Build.VERSION.SDK_INT >= 29) {

            contentValues.put(MediaStore.Images.Media.DATE_TAKEN, currentTime);

            contentValues.put(MediaStore.Images.Media.RELATIVE_PATH,
                    Environment.DIRECTORY_PICTURES + "/" + FOLDER_IMAGES);


        } else {

            // Todo ( Something equivalent to RELATIVE_PATH)

        }

        ImageCapture.OutputFileOptions options = new ImageCapture.OutputFileOptions.Builder(
                getContentResolver(), MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues).build();


        imageCapture.takePicture(options,
                ContextCompat.getMainExecutor(this),
                new ImageCapture.OnImageSavedCallback() {

                    @Override
                    public void onImageSaved(@NonNull ImageCapture.OutputFileResults results) {

                        ToastUtility.successToast(getApplicationContext(),
                                "Photo Capture Successfully");

                        showProgress(false);

                    }

                    @Override
                    public void onError(@NonNull ImageCaptureException exception) {

                        ToastUtility.errorToast(getApplicationContext(),
                                "Photo Couldn't Capture");

                        showProgress(false);

                    }
                });

    }

更新#1

通过以下代码解决了这个问题


        if (Build.VERSION.SDK_INT >= 29) {

            contentValues.put(MediaStore.Images.Media.DATE_TAKEN, currentTime);

            contentValues.put(MediaStore.Images.Media.RELATIVE_PATH,
                    Environment.DIRECTORY_PICTURES + "/" + FOLDER_IMAGES);


        } else {

            createFolderIfNotExist();

            String path = Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_PICTURES) + "/" + FOLDER_IMAGES + "/" + imageDisplayName;

            contentValues.put(MediaStore.Images.Media.DATA, path);

        }

    private void createFolderIfNotExist() {

        File file = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES) + "/" + FOLDER_IMAGES);


        if (!file.exists()) {

            if (!file.mkdir()) {

                Log.d(TAG, "Folder Create -> Failure");

            } else {

                Log.d(TAG, "Folder Create -> Success");
            }

        }

    }

标签: javaandroidandroid-camerax

解决方案


推荐阅读