Skip to content

Commit 5a709e0

Browse files
authored
Merge pull request #757 from limpbrains/init
Fixed #756 Catch camera init and trigger onError
2 parents 4892689 + 1371773 commit 5a709e0

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

android/src/main/java/com/rncamerakit/CKCamera.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,17 @@ class CKCamera(context: ThemedReactContext) : FrameLayout(context), LifecycleObs
186186
val cameraProviderFuture = ProcessCameraProvider.getInstance(getActivity())
187187
cameraProviderFuture.addListener({
188188
// Used to bind the lifecycle of cameras to the lifecycle owner
189-
cameraProvider = cameraProviderFuture.get()
189+
try {
190+
cameraProvider = cameraProviderFuture.get()
191+
} catch (exc: Exception) {
192+
val rootCause = exc.cause?.cause?.message ?: exc.cause?.message ?: exc.message ?: "Camera initialization failed"
193+
Log.e(TAG, "Camera initialization failed: $rootCause", exc)
194+
val surfaceId = UIManagerHelper.getSurfaceId(currentContext)
195+
UIManagerHelper
196+
.getEventDispatcherForReactTag(currentContext, id)
197+
?.dispatchEvent(ErrorEvent(surfaceId, id, rootCause))
198+
return@addListener
199+
}
190200

191201
// Rotate the image according to device orientation, even when UI orientation is locked
192202
orientationListener = object : OrientationEventListener(context, SensorManager.SENSOR_DELAY_UI) {

0 commit comments

Comments
 (0)