apache / cordova-plugin-camera

Apache Cordova Plugin camera
https://cordova.apache.org/
Apache License 2.0
966 stars 1.55k forks source link

[Android] Permission denied when trying to launch camera #471

Closed ilyakamens closed 4 years ago

ilyakamens commented 5 years ago

Bug Report

Problem

I'm unable to launch the camera on Android.

What is expected to happen?

The camera should launch.

What does actually happen?

The error callback is called with a value of 20, which I believe is PERMISSION_DENIED_ERROR: https://github.com/apache/cordova-plugin-camera/blob/master/src/android/CameraLauncher.java#L98

Information

It's the only call I make on the page (after device is ready, of course).

Command or Code

window.navigator.camera.getPicture(
  (data: any) =>  alert(`success: ${data}`),
  (err: any) => alert(`error: ${err}`),
);

Environment, Platform, Device

I've tried on an LG K20 PLUS running Android 7.0 and a Galaxy S9 running Android 9.

Version information

$ cordova --version
9.0.0 (cordova-lib@9.0.1)
$ cordova platforms
Installed platforms:
  android 8.0.0
  ios 5.0.0
$ cordova plugins list
cordova-custom-config 5.1.0 "cordova-custom-config"
cordova-plugin-add-swift-support 2.0.1 "AddSwiftSupport"
cordova-plugin-advanced-http 2.0.6 "Advanced HTTP plugin"
cordova-plugin-android-referrer 1.0.0 "cordova-plugin-android-referrer"
cordova-plugin-androidx 1.0.2 "cordova-plugin-androidx"
cordova-plugin-androidx-adapter 1.0.2 "cordova-plugin-androidx-adapter"
cordova-plugin-appsflyer-sdk 4.4.15 "AppsFlyer"
cordova-plugin-backbutton 0.3.0 "Backbutton"
cordova-plugin-camera 4.0.3 "Camera"
cordova-plugin-device 2.0.2 "Device"
cordova-plugin-dialogs 2.0.1 "Notification"
cordova-plugin-facebook4 4.2.1 "Facebook Connect"
cordova-plugin-file 6.0.1 "File"
cordova-plugin-firebase 2.0.5 "Google Firebase Plugin"
cordova-plugin-geolocation 4.0.1 "Geolocation"
cordova-plugin-globalization 1.11.0 "Globalization"
cordova-plugin-inappbrowser 3.1.0-dev "InAppBrowser"
cordova-plugin-ionic-webview 4.0.1 "cordova-plugin-ionic-webview"
cordova-plugin-keyboard 1.2.0 "Keyboard"
cordova-plugin-localtunnel 2.0.3-dev "LocalTunnel"
cordova-plugin-network-information 2.0.2-dev "Network Information"
cordova-plugin-request-location-accuracy 2.2.3 "Request Location Accuracy"
cordova-plugin-request-review 1.0.0 "Request Review"
cordova-plugin-shared-preferences 0.0.1 "Cordova Plugin for Android SharedPreferences"
cordova-plugin-sqlite-2 1.0.6 "SQLitePlugin"
cordova-plugin-statusbar 2.4.2 "StatusBar"
cordova-plugin-whitelist 1.3.3 "Whitelist"
cordova-plugin-x-socialsharing 5.4.4 "SocialSharing"
cordova.plugins.diagnostic 4.0.12 "Diagnostic"
es6-promise-plugin 4.2.2 "Promise"
ionic-plugin-deeplinks 1.0.20 "Ionic Deeplink Plugin"
phonegap-plugin-mobile-accessibility 1.0.5-dev "Mobile Accessibility"

Mac OS X 10.14.5

Checklist

ilyakamens commented 5 years ago

Update: I figured it out. We explicitly don't have the <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" permission in our AndroidManifest.xml. Specifically, we have a Cordova build hook that removes it, since we don't want to have to add that permission to our already-live app.

From looking at https://github.com/apache/cordova-plugin-camera/blob/master/src/android/CameraLauncher.java#L253, it appears we shouldn't actually need that permission to use this plugin if we just want to take a picture and not use the camera roll (i.e., just store the picture in Local Storage).

I think I'll have to fork this plugin to remove requesting that permission if I want to be able to use it without adding the WRITE_EXTERNAL_STORAGE permission to our app.

  1. Is my understanding of the issue correct?
  2. Is my understanding of a potential solution correct?
  3. Did I miss something in the documentation that says we need to add the WRITE_EXTERNAL_STORAGE permission to AndroidManifest.xml? If not, would be great to add.

Thanks!

janpio commented 5 years ago

I can only respond to one of these, as it is visible from the code:

3. Did I miss something in the documentation that says we need to add the WRITE_EXTERNAL_STORAGE permission to AndroidManifest.xml? If not, would be great to add.

The plugin adds this permission by default: https://github.com/apache/cordova-plugin-camera/blob/eed44330a45daefea4897eafe913838b0f8cfa67/plugin.xml#L60 So removing it might of course cause problems. (Which doesn't mean that the plugin couldn't be refactored in a way that would not make this necessary)

ilyakamens commented 5 years ago

Thanks. I'll either refactor and submit PR or fork. Sorry for the noise!

janpio commented 5 years ago

Actually not noise at all, very valid questions. I would actually say let's keep the issue open for a bit to see if you can get real answers from actual users of the plugin. I "only" do issue triage ;)

Arieru commented 5 years ago

Is it just me or this permission is really missing in the AndroidManifest.xml when using this plugin: <uses-permission android:name="android.permission.CAMERA" />

QuentinIcky commented 5 years ago

Any way to fix it ? Adding <uses-permission android:name="android.permission.CAMERA" /> doesn't change anything for me.

hungtranpg commented 5 years ago

Same issue on android 8.1.0 & cordova-plugin-camera 4.1.0 "Camera" navigator.camera.getPicture(onSuccess, onFail, options); onFail with message length=1; index=1

AmrAbdalrahman commented 4 years ago

@QuentinIcky same with me are you fix it?

QuentinIcky commented 4 years ago

@AmrAlmagic nope sorry :(

cleverappdesign commented 4 years ago

Having the same issue with cordova 9.0. The camera return 20 when try to access Gallery or take picture. Is there a fix for this?

Gregordy commented 4 years ago

Having the same issue with cordova 9.0. The camera return 20 when try to access Gallery or take picture. Is there a fix for this?

Same, I am on Cordova 9.0 (on Ionic 3.9.9) and trying to take a picture was resulting in an error 20. I then simply added the following permission to my config.xml and I was able to take a picture.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Also, if this can help, I didn't need to use the cordova-plugin-android-permissions.

Hope this helps!

jkenjarCR commented 1 year ago

This solution does not work for me. I have tried adding the permission and i get the same error.

davidgcs commented 1 year ago

This solution does not work for me. I have tried adding the permission and i get the same error.

Changing TargetSdkVersion from 33 to 32 worked for me