private void savePhoto() { if (imageCapture == null) return; if (isTakingPhoto.get()) return; showProgressBar(); hidePreviewImage(); if (cameraState == CameraState.IDLE) { try { startCamera(); } catch (Exception e) {e.printStackTrace(); } } Button captureButton = requireView().findViewById(R.id.camera_capture_button); captureButton.setEnabled(false); File photoFile = getPhotoFile(); ImageCapture.OutputFileOptions outputOptions = new ImageCapture.OutputFileOptions.Builder(photoFile) .build(); isTakingPhoto.set(true); imageCapture.takePicture( outputOptions, ContextCompat.getMainExecutor(requireContext()), new ImageCapture.OnImageSavedCallback() { @Override public void onImageSaved(@NonNull ImageCapture.OutputFileResults outputFileResults) { Uri savedUri = Uri.fromFile(photoFile); Log.d(TAG, "Photo is shot to " + savedUri.toString()); try { ExifInterface exifInterface = new ExifInterface(photoFile.getAbsolutePath()); ExifData exifData = loadExifData(exifInterface); photoSet.setExifData(exifData); cropAndCompressPhotoFile(photoFile); exifInterface.saveAttributes(); } catch (IOException e) { e.printStackTrace(); } photoSet.setPhotoTaken(true); if (photoSet.isBoxPhotoNext()) { shutdownCamera(); } photoSet.setFlash(flashButtonChecked); isTakingPhoto.set(false); captureButton.setEnabled(true); UpdateUI(); } @Override public void onError(@NonNull ImageCaptureException exception) { Log.e(TAG, "Photo capture is failed: " + exception.getLocalizedMessage()); exception.printStackTrace(); captureButton.setEnabled(true); isTakingPhoto.set(false); UpdateUI(); } }); }