From 2627638a766288b73bb58ee57195788b63d3e16c Mon Sep 17 00:00:00 2001 From: darthpaul Date: Thu, 29 Sep 2022 18:05:40 +0100 Subject: [PATCH] minor fixes for media output and toggling resolution - handle exception when creating media output - if only one resolution is supported, no need to restart the camera --- .../camera/helpers/MediaOutputHelper.kt | 74 +++++++++++-------- .../camera/implementations/CameraXPreview.kt | 24 +++--- 2 files changed, 54 insertions(+), 44 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/helpers/MediaOutputHelper.kt b/app/src/main/kotlin/com/simplemobiletools/camera/helpers/MediaOutputHelper.kt index cf269521..2088f0e9 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/helpers/MediaOutputHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/helpers/MediaOutputHelper.kt @@ -34,52 +34,62 @@ class MediaOutputHelper( private val contentResolver = activity.contentResolver fun getImageMediaOutput(): MediaOutput { - return if (is3rdPartyIntent) { - if (outputUri != null) { - val outputStream = openOutputStream(outputUri) - if (outputStream != null) { - MediaOutput.OutputStreamMediaOutput(outputStream, outputUri) + return try { + if (is3rdPartyIntent) { + if (outputUri != null) { + val outputStream = openOutputStream(outputUri) + if (outputStream != null) { + MediaOutput.OutputStreamMediaOutput(outputStream, outputUri) + } else { + errorHandler.showSaveToInternalStorage() + getMediaStoreOutput(isPhoto = true) + } } else { - errorHandler.showSaveToInternalStorage() - getMediaStoreOutput(isPhoto = true) + MediaOutput.BitmapOutput } } else { - MediaOutput.BitmapOutput + getOutputStreamMediaOutput() ?: getMediaStoreOutput(isPhoto = true) } - } else { - getOutputStreamMediaOutput() ?: getMediaStoreOutput(isPhoto = true) + } catch (e: Exception) { + errorHandler.showSaveToInternalStorage() + getMediaStoreOutput(isPhoto = true) } } fun getVideoMediaOutput(): MediaOutput { - return if (is3rdPartyIntent) { - if (outputUri != null) { - if (isOreoPlus()) { - val fileDescriptor = openFileDescriptor(outputUri) - if (fileDescriptor != null) { - MediaOutput.FileDescriptorMediaOutput(fileDescriptor, outputUri) + return try { + if (is3rdPartyIntent) { + if (outputUri != null) { + if (isOreoPlus()) { + val fileDescriptor = openFileDescriptor(outputUri) + if (fileDescriptor != null) { + MediaOutput.FileDescriptorMediaOutput(fileDescriptor, outputUri) + } else { + errorHandler.showSaveToInternalStorage() + getMediaStoreOutput(isPhoto = false) + } } else { - errorHandler.showSaveToInternalStorage() - getMediaStoreOutput(isPhoto = false) + val path = activity.getRealPathFromURI(outputUri) + if (path != null) { + MediaOutput.FileMediaOutput(File(path), outputUri) + } else { + errorHandler.showSaveToInternalStorage() + getMediaStoreOutput(isPhoto = false) + } } } else { - val path = activity.getRealPathFromURI(outputUri) - if (path != null) { - MediaOutput.FileMediaOutput(File(path), outputUri) - } else { - errorHandler.showSaveToInternalStorage() - getMediaStoreOutput(isPhoto = false) - } + getMediaStoreOutput(isPhoto = false) } } else { - getMediaStoreOutput(isPhoto = false) - } - } else { - if (isOreoPlus()) { - getFileDescriptorMediaOutput() ?: getMediaStoreOutput(isPhoto = false) - } else { - getFileMediaOutput() ?: getMediaStoreOutput(isPhoto = false) + if (isOreoPlus()) { + getFileDescriptorMediaOutput() ?: getMediaStoreOutput(isPhoto = false) + } else { + getFileMediaOutput() ?: getMediaStoreOutput(isPhoto = false) + } } + } catch (e: Exception) { + errorHandler.showSaveToInternalStorage() + getMediaStoreOutput(isPhoto = false) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/implementations/CameraXPreview.kt b/app/src/main/kotlin/com/simplemobiletools/camera/implementations/CameraXPreview.kt index 2a138e8b..db17a54f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/implementations/CameraXPreview.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/implementations/CameraXPreview.kt @@ -373,18 +373,19 @@ class CameraXPreview( } private fun toggleResolutions(resolutions: List) { - val currentIndex = mediaSizeStore.getCurrentSizeIndex(isPhotoCapture, isFrontCameraInUse()) + if (resolutions.size >= 2) { + val currentIndex = mediaSizeStore.getCurrentSizeIndex(isPhotoCapture, isFrontCameraInUse()) - val nextIndex = if (currentIndex >= resolutions.lastIndex) { - 0 - } else { - currentIndex + 1 + val nextIndex = if (currentIndex >= resolutions.lastIndex) { + 0 + } else { + currentIndex + 1 + } + + mediaSizeStore.storeSize(isPhotoCapture, isFrontCameraInUse(), nextIndex) + currentRecording?.stop() + startCamera() } - - mediaSizeStore.storeSize(isPhotoCapture, isFrontCameraInUse(), nextIndex) - currentRecording?.stop() - startCamera() - } override fun toggleFrontBackCamera() { @@ -438,7 +439,7 @@ class CameraXPreview( val mediaOutput = mediaOutputHelper.getImageMediaOutput() if (mediaOutput is MediaOutput.BitmapOutput) { - imageCapture.takePicture(mainExecutor, object : ImageCapture.OnImageCapturedCallback() { + imageCapture.takePicture(mainExecutor, object : OnImageCapturedCallback() { override fun onCaptureSuccess(image: ImageProxy) { listener.toggleBottomButtons(false) val bitmap = BitmapUtils.makeBitmap(image.toJpegByteArray()) @@ -457,7 +458,6 @@ class CameraXPreview( val outputOptionsBuilder = when (mediaOutput) { is MediaOutput.MediaStoreOutput -> OutputFileOptions.Builder(contentResolver, mediaOutput.contentUri, mediaOutput.contentValues) is MediaOutput.OutputStreamMediaOutput -> OutputFileOptions.Builder(mediaOutput.outputStream) - is MediaOutput.BitmapOutput -> throw IllegalStateException("Cannot produce an OutputFileOptions for a bitmap output") else -> throw IllegalArgumentException("Unexpected option for image ") }