From 3850491b9f443257880964a4b0c0c70f484ab4f3 Mon Sep 17 00:00:00 2001 From: Menardi Date: Mon, 4 Apr 2022 10:03:08 +0100 Subject: [PATCH] fix: Fix takeSnapshot not working on Android (#961) Accessing previewView.bitmap was throwing an error because it wasn't being done on the main thread. Any access to previewView needs to be done on the main (UI) thread. This commit fixes the issue by ensuring this access is now run on the main thread. Fixes #547 --- .../main/java/com/mrousavy/camera/CameraView+TakeSnapshot.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/android/src/main/java/com/mrousavy/camera/CameraView+TakeSnapshot.kt b/android/src/main/java/com/mrousavy/camera/CameraView+TakeSnapshot.kt index 461824a..3f3299b 100644 --- a/android/src/main/java/com/mrousavy/camera/CameraView+TakeSnapshot.kt +++ b/android/src/main/java/com/mrousavy/camera/CameraView+TakeSnapshot.kt @@ -22,7 +22,9 @@ suspend fun CameraView.takeSnapshot(options: ReadableMap): WritableMap = corouti camera.cameraControl.enableTorch(true).await() } - val bitmap = this@takeSnapshot.previewView.bitmap ?: throw CameraNotReadyError() + val bitmap = withContext(coroutineScope.coroutineContext) { + previewView.bitmap ?: throw CameraNotReadyError() + } val quality = if (options.hasKey("quality")) options.getInt("quality") else 100