android 带有cameraX的媒体商店不会覆盖保存的照片

yvfmudvl  于 12个月前  发布在  Android
关注(0)|答案(1)|浏览(218)

我想照片被覆盖,但相反(2),(3)等出现在每张照片旁边
我尝试设置永久保存位置,但不起作用。这是我用来捕获和保存照片的代码:
void bindPreview(@ NoncodeProcessCameraProvider cameraProvider){ Preview preview = new Preview.Builder().build(); CameraCapture. Builder builder = new CameraCapture.Builder().requireLensFacing(CameraCapture.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);
    }
}

字符串

cig3rfwq

cig3rfwq1#

您仍然在使用contentResolver.insert调用插入新图像。
要更新现有映像,请获取OutputStream并使用它构建OutputFileOptions:

ContentResolver contentResolver = getContentResolver();

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

字符串

相关问题