Harden CameraManager error handling
- Catch SecurityException separately with permission-specific message - Replace raw e.message with generic user-friendly error strings - Wrap cameraProviderFuture.get() in try-catch to handle CameraX initialization failures instead of crashing Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
cd51c1a843
commit
18a4289b96
1 changed files with 12 additions and 4 deletions
|
|
@ -73,9 +73,14 @@ class CameraManager(private val context: Context) {
|
|||
|
||||
val cameraProviderFuture = ProcessCameraProvider.getInstance(context)
|
||||
cameraProviderFuture.addListener({
|
||||
try {
|
||||
cameraProvider = cameraProviderFuture.get()
|
||||
lensController.initialize(cameraProvider?.availableCameraInfos ?: emptyList())
|
||||
bindCameraUseCases(lifecycleOwner)
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "CameraX initialization failed", e)
|
||||
_error.value = "Camera could not initialize. Please restart the app."
|
||||
}
|
||||
}, ContextCompat.getMainExecutor(context))
|
||||
}
|
||||
|
||||
|
|
@ -129,9 +134,12 @@ class CameraManager(private val context: Context) {
|
|||
provideSurface(request)
|
||||
}
|
||||
|
||||
} catch (e: SecurityException) {
|
||||
Log.e(TAG, "Camera permission denied at runtime", e)
|
||||
_error.value = "Camera permission was revoked. Please grant it in Settings."
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Camera binding failed", e)
|
||||
_error.value = "Camera failed: ${e.message}"
|
||||
_error.value = "Camera could not start. Please try again or restart the app."
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue