Modern Apps git

← Modern-Apps.git

camera: fix image-viewer crash on missing image and reshape aspect-ratio preview

Commit
bc9ac07b2df61c17b6e4e8efdf6a25ba5a525251
Author
Vayun Mathur <[email protected]>
When
2026-07-30 16:07 UTC (4 months ago)
Parents
dc2b1a6

Diff

camera/src/main/java/com/vayunmathur/camera/ui/CameraScreen.kt
diff --git a/camera/src/main/java/com/vayunmathur/camera/ui/CameraScreen.kt b/camera/src/main/java/com/vayunmathur/camera/ui/CameraScreen.ktindex 66c2a195dc..cc4ebf8501 100644--- a/camera/src/main/java/com/vayunmathur/camera/ui/CameraScreen.kt+++ b/camera/src/main/java/com/vayunmathur/camera/ui/CameraScreen.kt@@ -685,7 +685,10 @@ fun CameraScreen(                             implementationMode = ImplementationMode.EMBEDDED,                             coordinateTransformer = coordinateTransformer,                             alignment = Alignment.Center,-                            contentScale = if (isSloMo) ContentScale.Crop else ContentScale.Fit,+                            // Crop (fill) so the camera feed fills the ratio-shaped box for the+                            // selected aspect ratio (1:1 / 16:9 / 4:3). Fit letterboxed the native+                            // 4:3 frame inside the box, making the preview shrink instead of reshape.+                            contentScale = ContentScale.Crop,                             // Built-in tap-to-focus and pinch-to-zoom (1.7.0-alpha02). The viewfinder                             // applies focus/zoom on the SurfaceRequest's camera; we just sync the                             // displayed zoom ratio so the zoom bar stays in step.@@ -840,13 +843,23 @@ fun CameraScreen(                     onSnapshot = { viewModel.captureVideoSnapshot() },                     onFlipCamera = { viewModel.flipCamera() },                     onGallery = {-                        val intent = lastCaptureUri?.let { uri ->-                            Intent(Intent.ACTION_VIEW).apply {-                                setDataAndType(uri, context.contentResolver.getType(uri) ?: "image/*")-                                addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)+                        // A last-capture URI can be stale (image deleted) or absent (nothing shot+                        // this session). Verify it still resolves, else open the gallery collection;+                        // wrap the whole launch so a missing image never crashes the app.+                        runCatching {+                            val uri = lastCaptureUri?.takeIf {+                                runCatching { context.contentResolver.getType(it) }.getOrNull() != null                             }-                        } ?: Intent(Intent.ACTION_VIEW, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)-                        context.startActivity(Intent.createChooser(intent, null))+                            val intent = if (uri != null) {+                                Intent(Intent.ACTION_VIEW).apply {+                                    setDataAndType(uri, context.contentResolver.getType(uri) ?: "image/*")+                                    addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)+                                }+                            } else {+                                Intent(Intent.ACTION_VIEW, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)+                            }+                            context.startActivity(Intent.createChooser(intent, null))+                        }.onFailure { Log.w("CameraScreen", "Could not open image viewer", it) }                     },                     iconRotation = animatedRotation,                     flipEnabled = !isSloMo
camera/src/main/java/com/vayunmathur/camera/util/CameraViewModel.kt
diff --git a/camera/src/main/java/com/vayunmathur/camera/util/CameraViewModel.kt b/camera/src/main/java/com/vayunmathur/camera/util/CameraViewModel.ktindex 108b553f76..6b190d0953 100644--- a/camera/src/main/java/com/vayunmathur/camera/util/CameraViewModel.kt+++ b/camera/src/main/java/com/vayunmathur/camera/util/CameraViewModel.kt@@ -668,7 +668,9 @@ class CameraViewModel(private val app: Application) : AndroidViewModel(app) {             }         }         ds.getString("camera_location")?.let { _locationEnabled.value = it.toBoolean() }-        ds.getString("camera_last_capture")?.let { _lastCaptureUri.value = Uri.parse(it) }+        // Guard against a blank persisted value: Uri.parse("") yields a non-null+        // but invalid URI, making the app think an image exists when none does.+        ds.getString("camera_last_capture")?.takeIf { it.isNotBlank() }?.let { _lastCaptureUri.value = Uri.parse(it) }         ds.getString("camera_grid")?.let { _gridEnabled.value = it.toBoolean() }         ds.getString("camera_level")?.let { _levelEnabled.value = it.toBoolean() }         ds.getString("camera_mic_muted")?.let { _micMuted.value = it.toBoolean() }