cordova-plugin-camera: Android 13 : Camera/Gallery doesn't open
Bug Report
Msg: cannot open camera: 20
Problem
Camera doesn’t open, Gallery doesn’t open, for Android 13 emulator Pointing API 33 of Android in the build.gradle
What is expected to happen?
Camera should open, Gallery should open
What does actually happen?
Nothing, display a bug message : Msg: cannot open camera: 20
E/Capacitor/Browser: Error binding to custom tabs service
Command or Code
const options: CameraOptions = {
quality: 100,
destinationType: this.camera.DestinationType.DATA_URL,
sourceType: this.camera.PictureSourceType.SAVEDPHOTOALBUM,
correctOrientation: true,
allowEdit: false,
targetHeight: 1280,
targetWidth: 1280,
};
this.camera.getPicture(options).then()....
Environment, Platform, Device
Ionic, Android 13
Workarounds
Use API 32
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 8
- Comments: 42 (1 by maintainers)
After investigating, the error comes from this method on CameraLauncher.java file :
If I comment this section about comparing value of r and PackageManager.PERMISSION_DENIED value, camera and gallery work good. It means we are always getting -1 value even if we allow permissions. I’m not Android Expert but i try to get explanations, how to fix this permission section
For what it’s worth, using
https://github.com/felicienfrancois/cordova-plugin-camera#e6e49b85b59263fc5782e835160d01a8932e3690
solved my error 20 problem on API 33 as well.@jkainth that PR is working OK, you can install that PR, but you can use also @felicienfrancois repo, it’s working just fine, I’m using it in two of my projects
github:felicienfrancois/cordova-plugin-camera
Confirmed on pure cordova app
Cordova android v11.0.0 cordova camera plugin v6.0.0
Build Tools / Target SDK 33 on Android 13 emulator
Error code 20 is produced to the JS console
Native logcat:
At a glance, it appears something to do with handling permissions has changed in Android 13, as error code
20
is a Permission Error Code…https://github.com/apache/cordova-plugin-camera/blob/4608f8ef8027a5c7130a3479c2d37a5e391c70e8/src/android/CameraLauncher.java#L100
I’ve also tested against build tools / target SDK 32 on an Android 13 emulator which appears to work fine. So this appears to be exclusively an API 33 issue.
@Houdhey @keva91 this issue was already discussed on the PR. It is due to manifest merge conflicts with other plugins using WRITE_EXTERNAL_STORAGE permission without setting android:maxSdkVersion=“32” in their manifest.
As discussed on the PR #814 , the android:maxSdkVersion=“32” constraint should be removed from the PR as it would create issues for most people.
Also, from my tests, #814 PR has also another issue with DATA_URI output.
I forked this PR to solve both issues here : https://github.com/felicienfrancois/cordova-plugin-camera
Is there any work to actually fix this issue? The solution above me doesn’t seem that fitting since it can crash the application if the user declines to give access to the camera/gallery. Is there a reason why all the
grantResults
are-1
for android 13?@jfoclpf Yes. Specifying a min or max sdk version creates conflicts with other plugins, even some officail plugins. It seems that not specifying any min or max sdk version does not create any issue (I think it is ignored by versions not supporting this permission), but it still need to be validated. As a result, I would recommend to remove minSdkVersion and maxSdkVersion from the PR
@keva91 thank you for your feedback, I have not submitted my fork as a PR yet because it deserve tests and adjustment on the required permissions at runtime, depending on case. I personaly use it with success, in DATA_URI mode.
Can you tell me what parameters do you use ? Which mode ? DATA_URI ? FILE ? Taking photo or picking from library ?
Also, you can try thoose different versions :
READ_MEDIA_IMAGES Always requested (Same as PR #814 )
https://github.com/felicienfrancois/cordova-plugin-camera#f53d3cb2ef5add8f7ba37503c4a10fc8e1337600
READ_MEDIA_IMAGES Never requested (The one I use)
https://github.com/felicienfrancois/cordova-plugin-camera#e6e49b85b59263fc5782e835160d01a8932e3690
READ_MEDIA_IMAGES requested only when picking from library (Head, i.e. the one you tried)
https://github.com/felicienfrancois/cordova-plugin-camera#efcd9190d98130d815b442b96e0c69fe17637908
As far as I understand, the READ_MEDIA_IMAGES should not be requested as it may not be granted (even refused without prompt in my case) and it is not necessary, at least in my case. But maybe it is needed when using FILE mode
@keva91 ok, thank you. So PR #814 has definitelly an issue with READ_MEDIA_IMAGES. According to android doc this permission is required when accessing images on external storage or created by other apps so there may still be cases where it is needed. I just made another change on my fork (adding READ_MEDIA_IMAGES to manifest), if you want to test it:
https://github.com/felicienfrancois/cordova-plugin-camera#7d66143ed55d817676c10a460c66938519d4b85b
Because Google changed permission requirements with Android 13
https://developer.android.com/about/versions/13/behavior-changes-13?hl=en
hello… any update for this issue.