Cordova Android - 无法打开相机 - READ_EXTERNAL_STORAGE

时间:2017-08-04 14:21:06

标签: android cordova permissions

我试图在我的Cordova应用程序上打开相机,因为它将其移至Android Studio,现在它已停止工作。

我没有弹出任何权限,或者用户端没有任何内容向我显示。

我在Android监视器中收到以下错误

com.ontrac.nutshellmobile E/ViewRootImpl: sendUserActionEvent() returned.
E/DatabaseUtils: Writing exception to parcel
java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=8324, uid=10182 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
at android.content.ContentProvider.enforceReadPermissionInner(ContentProvider.java:616)
at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:487)
at android.content.ContentProvider$Transport.query(ContentProvider.java:216)
at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:112)
at android.os.Binder.execTransact(Binder.java:573)

我的清单具有以下权限

 <uses-sdk android:minSdkVersion="23" android:targetSdkVersion="26" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-feature android:name="android.hardware.telephony" android:required="false" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.RECORD_VIDEO" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

我正在为项目结构上的编译SDK版本运行API 26.

我的构建gradle文件包含

的SDK版本
minSdkVersion 23
targetSdkVersion 25
compileSdkVersion 26
buildToolsVersion '26.0.0'

我已尝试将权限移至Cordova Android清单但没有运气。我似乎无法弄清楚导致错误的原因。

非常感谢任何帮助。如果需要,我可以提供更多信息。

1 个答案:

答案 0 :(得分:0)

要打开相机,您必须安装 cordova-plugin-android-permissions

您必须要求2个权限: READ_EXTERNAL_STORAGE CAMERA ,并在2个功能的回调函数中打开相机。

js代码示例:

function takePicture() {
  var permissions = cordova.plugins.permissions;
  permissions.requestPermission(permissions.READ_EXTERNAL_STORAGE,
    function (status) {

      if (!status.hasPermission)
       error();
      else
       permissions.requestPermission(permissions.CAMERA, success.bind(this),
      error);

      function error() {
        console.warn('CAMERA permission is not turned on');
      }
    }
    , error);

    function error() {
      console.warn('READ_EXTERNAL_STORAGE permission is not turned on');
    }

    function success(status) {
      if (!status.hasPermission)
       error();
      else
       openCamera();
    }
  }

  function openCamera() {
    navigator.camera.getPicture(onSuccess, onFail, {
      quality: 25,
      destinationType: Camera.DestinationType.DATA_URL
    });

    function onSuccess(imageData) {

      var src = "data:image/png;base64," + imageData;
      console.log(src);
      alert('got picture!!!');
    }

    function onFail(message) {
      alert('error ' + message);
    }
  }

  takePicture();