Mediastore with cameraX does not overwrite saved photos

46 Views Asked by At

I wanted the photos to be overwritten, but instead (2), (3) etc. appear next to each photo

I tried setting a permanent save location, but it didn't work. This is the code i'm using to capture and save my pictures:

void bindPreview(@NonNull ProcessCameraProvider cameraProvider) { Preview preview = new Preview.Builder().build(); CameraSelector cameraSelector = new CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK).build(); ImageAnalysis imageAnalysis = new ImageAnalysis.Builder().build(); ImageCapture.Builder builder = new ImageCapture.Builder();

    final ImageCapture imageCapture = builder.setTargetRotation(this.getWindowManager().getDefaultDisplay().getRotation()).build();

    preview.setSurfaceProvider(binding.cameraPreviewView.getSurfaceProvider());
    cameraProvider.bindToLifecycle((LifecycleOwner) this, lensFacing, preview, imageAnalysis, imageCapture);
    String relativePath = Environment.DIRECTORY_PICTURES + File.separator + "Photometer" + File.separator + "captured_image";
    // Create temporary file
    ContentValues contentValues = new ContentValues();
    contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, "captured_image");
    contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg");
    contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, relativePath);

    // Use ContentResolver to insert the image and get the content URI
    ContentResolver contentResolver = getContentResolver();
    imageUri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);

    if (buttonState == 1) {
        Timer my_timer = new Timer();
        my_timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                ImageCapture.OutputFileOptions outputFileOptions = new ImageCapture.OutputFileOptions.Builder(
                        contentResolver, imageUri, null
                ).build();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (buttonState == 0) {
                            return;
                        }
                        try {
                            imageCapture.takePicture(outputFileOptions, executor, new ImageCapture.OnImageSavedCallback() {
                                @Override
                                public void onImageSaved(@NonNull ImageCapture.OutputFileResults outputFileResults) {
                                }

                                @Override
                                public void onError(@NonNull ImageCaptureException error) {
                                    error.printStackTrace();
                                }
                            });
                            FastAnalyzePhoto();
                            if (timerCancel == 1) {
                                cameraProvider.unbindAll();
                                my_timer.cancel();
                                binding.realtimeProgress.setVisibility(GONE);
                                startCamera();
                            }
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }
                });
            }
        }, 0, IMAGE_ANALYSE_REFRESH_DELAY_MS);
    }
}
1

There are 1 best solutions below

0
Xi 张熹 On

You are still inserting a new image with the contentResolver.insert call.

To update the existing image, get the OutputStream and build a OutputFileOptions with it:

ContentResolver contentResolver = getContentResolver();

try {
    // Open an OutputStream to write into the existing image's Uri
    OutputStream outputStream = contentResolver.openOutputStream(existingImageUri);
...