Save image to a specific folder using Camera X ImageCapture.OutputFileOptions.Builder below android 29

Viewed 879

I am trying to implement Camera X app. The problem i am facing right now is i am not able to figure out how to save image below to a specific folder using ImageCapture.OutputFileOptions.Builder

My current code is below. Do i need to adopt some other way? Or i can also do in this way?


    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);

                    }
                });

    }

Update # 1

Solved this by following code


        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");
            }

        }

    }

0 Answers
Related