@@ -36,6 +36,7 @@ import kotlinx.coroutines.Job
3636import kotlinx.coroutines.SupervisorJob
3737import kotlinx.coroutines.delay
3838import kotlinx.coroutines.launch
39+ import kotlinx.coroutines.withContext
3940import org.json.JSONArray
4041import org.json.JSONObject
4142import java.io.File
@@ -926,25 +927,20 @@ constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
926927 val cameraProviderFuture = ProcessCameraProvider .getInstance(context)
927928 cameraProviderFuture.addListener(
928929 {
929- try {
930- cameraProvider = cameraProviderFuture.get()
931- val extensionsManagerFuture =
932- ExtensionsManager .getInstanceAsync(context, cameraProvider!! )
933- extensionsManagerFuture.addListener({
934- try {
935- extensionsManager = extensionsManagerFuture.get()
936- bindindProvider = false
930+ CoroutineScope (Dispatchers .IO ).launch {
931+ try {
932+ cameraProvider = cameraProviderFuture.get()
933+ extensionsManager =
934+ ExtensionsManager .getInstanceAsync(context, cameraProvider!! ).get()
935+ bindindProvider = false
936+ withContext(Dispatchers .Main ) {
937937 refreshCamera()
938- } catch (e: Exception ) {
939- e.printStackTrace()
940- listener?.onCameraError(" Failed to get camera" , e)
941- isStarted = false
942938 }
943- }, ContextCompat .getMainExecutor(context))
944- } catch (e : Exception ) {
945- e.printStackTrace( )
946- listener?.onCameraError( " Failed to get camera " , e)
947- isStarted = false
939+ } catch (e : Exception ) {
940+ e.printStackTrace()
941+ listener?.onCameraError( " Failed to get camera " , e )
942+ isStarted = false
943+ }
948944 }
949945 },
950946 ContextCompat .getMainExecutor(context)
0 commit comments