I have camera in my project to take video with voice and save this in device folder. I am using Dexter library for permission check. Camera open on device with higher than api 32. but lower api 33 it's not working. I check this in debug mode that's not work in Dexter code if it's granted or not. What's wrong here?
AndroidManifest.xml
<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-feature android:name="android.hardware.camera.any" android:required="false" />
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false" />
<uses-feature android:name="android.hardware.camera.flash" android:required="false" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-feature android:name="android.permission.MANAGE_EXTERNAL_STORAGE" android:required="false" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="28" />
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />
Fragment.kt
private fun initCamera() {
cameraExecutor = Executors.newSingleThreadExecutor()
requestCameraPermission()
}
private fun requestCameraPermission() {
Dexter.withContext(requireContext())
.withPermissions(
Manifest.permission.RECORD_AUDIO,
Manifest.permission.CAMERA
).withListener(object : MultiplePermissionsListener {
override fun onPermissionsChecked(p0: MultiplePermissionsReport?) {
p0?.let {
if (it.areAllPermissionsGranted()) {
startCamera(CameraSelector.DEFAULT_BACK_CAMERA)
}
}
}
override fun onPermissionRationaleShouldBeShown(
p0: MutableList<PermissionRequest>?,
p1: PermissionToken?
) {
p1?.continuePermissionRequest()
}
}).check()
}
private fun startCamera(cameraSelector: CameraSelector) {
val cameraProviderFuture = ProcessCameraProvider.getInstance(requireContext())
val cameraProvider: ProcessCameraProvider = cameraProviderFuture.get()
cameraProviderFuture.addListener({
// Preview
val preview = Preview.Builder()
.build()
.also {
binding.previewViewTalkerDuelCreateNewDuelTakeVideo.implementationMode =
PreviewView.ImplementationMode.COMPATIBLE
it.setSurfaceProvider(
binding.previewViewTalkerDuelCreateNewDuelTakeVideo.surfaceProvider
)
}
val recorder = Recorder.Builder()
.setQualitySelector(
QualitySelector.from(
Quality.HIGHEST,
FallbackStrategy.higherQualityOrLowerThan(Quality.SD)
)
)
.build()
videoCapture = VideoCapture.withOutput(recorder)
try {
// Unbind use cases before rebinding
cameraProvider.unbindAll()
// Bind use cases to camera
cameraProvider.bindToLifecycle(
this, cameraSelector, preview, videoCapture
)
} catch (exc: Exception) {
exc.printStackTrace()
}
}, ContextCompat.getMainExecutor(requireContext()))
}
Dexter version:
implementation 'com.karumi:dexter:6.2.3'
I added these code for permission api lower than 33. And camera works now. So I just added answer who has same error with me.
private fun checkPermissions(permissions: Array<String>): Boolean {
for (permission in permissions) {
if (ContextCompat.checkSelfPermission(requireContext(), permission) != PackageManager.PERMISSION_GRANTED) {
return false
}
}
return true
}
private val requestMultiplePermissions =registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions())
{
permissions ->
permissions.entries.forEach {
it
}
if (permissions[RECORD_AUDIO] == true && permissions[CAMERA] == true) {
startCamera(CameraSelector.DEFAULT_BACK_CAMERA)
}
}