'Try-Catch doesn't catch Exception in Kotlin Android
Some Android devices don't have the QualitySelector.getSupportedQualities() method and I need to catch this error to give it a proper quality. But the try catch does not catch the exception and the application hangs,
Capture code:
try{
videoCapture = getVideoCapture()
camera = cameraProvider.bindToLifecycle(lifecycleOwner, cameraSelector, imageCapture,videoCapture,preview)
preview.setSurfaceProvider(binding.previewView.surfaceProvider)
}catch(e : Exception){
cameraProvider.unbindAll()
videoCapture = getVideoCaptureSD()
camera = cameraProvider.bindToLifecycle(lifecycleOwner, cameraSelector, imageCapture,videoCapture,preview)
preview.setSurfaceProvider(binding.previewView.surfaceProvider)
}
It is executed inside the bindPreview() method in a Runnable
cameraProviderFuture.addListener(Runnable {
val cameraProvider = cameraProviderFuture.get()
bindPreview(cameraProvider)}, ContextCompat.getMainExecutor(requireContext()))
Even now change the implementation, create an Executor and add it to the setSurfaceProvider method
private val executor = Executor { command ->
command?.let {
CoroutineScope(Dispatchers.Main).launch {
try{
val job = async(SupervisorJob()){it.run()}
job.await()
}catch (e : Exception){
Toast.makeText(requireContext(), "Exception", Toast.LENGTH_SHORT).show()
}
}
}
}
I did it the normal way and it didn't work for me, I did it with coroutines to be able to control the job but I didn't get the exception either :(
preview.setSurfaceProvider(executor, binding.previewView.surfaceProvider)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
