cordova-plugin-camera: Permission Denial: reading com.android.providers.media.MediaDocumentsProvider requires android.permission.MANAGE_DOCUMENTS

浪尽此生 提交于 2020-01-07 09:03:09

问题


I have an ionic mobile app and in my app, I want to be able to select images from gallery but I get this error after selecting the image

java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaDocumentsProvider uri content://com.android.providers.media.documents/document/image:58 from pid=2173, uid=10062 requires android.permission.MANAGE_DOCUMENTS, or grantUriPermission()

this is my code to select image from gallery and add it

var options = {
      destinationType : Camera.DestinationType.FILE_URI,
      sourceType : Camera.PictureSourceType.PHOTOLIBRARY,
      allowEdit : false,
      encodingType: Camera.EncodingType.JPEG,
      popoverOptions: CameraPopoverOptions,
      mediaType: Camera.MediaType.PICTURE,
      correctOrientation: true
    };

   // 3
    $cordovaCamera.getPicture(options).then(function(imageData) {
   // 4
      var imagetype;
      onImageSuccess(imageData);
      function onImageSuccess(fileURI) {
        console.log("im in image success")
        console.log(fileURI)
        createFileEntry(fileURI);
      }

      function createFileEntry(fileURI) {
        console.log("im in create file entry")
        console.log(fileURI)
        window.resolveLocalFileSystemURL(fileURI, copyFile, fail);
      }

       // 5
      function copyFile(fileEntry) {
        console.log("im in file entry")
        console.log(fileEntry)
        var name = fileEntry.fullPath.substr(fileEntry.fullPath.lastIndexOf('/') + 1);
        var newName = (new Date()).getTime() + name;
         halfthru(fileEntry, newName); //diff
         getImageType(fileEntry); //diff

      }

      function getImageType(fileEntry) { //diff
        var typeImage;
        $scope.$evalAsync(function() {
          fileEntry.file(function(file){
            console.log(fileEntry.file)
            console.log(file)
            typeImage= file.type;
            $scope.imagelist = typeImage;
            imagetype = typeImage;
          }, function(e){
          });
        })

      }

     function halfthru(fileEntry, newName) {
      console.log("halftru")
        window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function(fileSystem2) {
          fileEntry.copyTo(
            fileSystem2,
            newName,
            onCopySuccess,
            fail
          );
        }, fail);
      }

      // 6
      function onCopySuccess(entry) {
        console.log("im in copy success")
        console.log(entry)
          $scope.activeSlide = index;
          if(modalExists === false) {
            $ionicModal.fromTemplateUrl('image-modal.html', {
              scope: $scope,
            }).then(function(modal) {

              $scope.modal1 = modal;
              $scope.modal1.show();
              modalExists = true;
              $scope.$evalAsync($scope.images.push({file: entry.nativeURL, type: $scope.imagelist}));
              imagesModalCount =  $scope.images.length;
              attachedImageCount = $scope.imagesAttached.length;
            });
          }
          else {
            $scope.modal1.show();
            $scope.$evalAsync($scope.images.push({file: entry.nativeURL, type: $scope.imagelist}));
            imagesModalCount =  $scope.images.length;
            attachedImageCount  = $scope.imagesAttached.length;
          }

          $scope.$on('$destroy', function() {
            $scope.modal1.remove();
          });

      }

      function fail(error) {

      }
    }, function(err) {

    });

my config.xml file

<platform name="android">
    <icon src="resources/android/icon/drawable-ldpi-icon.png" density="ldpi"/>
    <icon src="resources/android/icon/drawable-mdpi-icon.png" density="mdpi"/>
    <icon src="resources/android/icon/drawable-hdpi-icon.png" density="hdpi"/>
    <icon src="resources/android/icon/drawable-xhdpi-icon.png" density="xhdpi"/>
    <icon src="resources/android/icon/drawable-xxhdpi-icon.png" density="xxhdpi"/>
    <icon src="resources/android/icon/drawable-xxxhdpi-icon.png" density="xxxhdpi"/>
    <splash src="resources/android/splash/drawable-land-ldpi-screen.png" density="land-ldpi"/>
    <splash src="resources/android/splash/drawable-land-mdpi-screen.png" density="land-mdpi"/>
    <splash src="resources/android/splash/drawable-land-hdpi-screen.png" density="land-hdpi"/>
    <splash src="resources/android/splash/drawable-land-xhdpi-screen.png" density="land-xhdpi"/>
    <splash src="resources/android/splash/drawable-port-ldpi-screen.png" density="port-ldpi"/>
    <splash src="resources/android/splash/drawable-port-mdpi-screen.png" density="port-mdpi"/>
    <splash src="resources/android/splash/drawable-port-hdpi-screen.png" density="port-hdpi"/>
    <splash src="resources/android/splash/drawable-port-xhdpi-screen.png" density="port-xhdpi"/>
    <splash src="resources/android/splash/drawable-port-xxhdpi-screen.png" density="port-xxhdpi"/>
    <splash src="resources/android/splash/drawable-port-xxxhdpi-screen.png" density="port-xxxhdpi"/>
    <config-file target="AndroidManifest.xml" parent="/*">
      <uses-permission platform="android" name="android.permission.MANAGE_DOCUMENTS" />
        </config-file>
  </platform>

This is happening on an android 5.1.1 phone.

My guess will be this is caused somehow from the cordova-file-plugin when resolveLocalFileSystemURL method is called but how do i solve this?


回答1:


It says that you need MANAGE_DOCUMENTS permissions, have you add this permission in your config.xml ?

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

In my app, to add an image from gallery I do:

$scope.addMedia = function addMedia() {
    var buttons = [];
    var onCameraSuccess = function onCameraSuccess(imageData) {
        $scope.files.push(imageData);
    };

    var onCameraError = function onCameraError() {};

    var photoConfig = {
        quality: 70,
        targetWidth: 400,
        targetHeight: 400,
        allowEdit: false,
        destinationType: window.navigator.camera.DestinationType.DATA_URL,
        encodingType: window.navigator.camera.EncodingType.JPEG,
        saveToPhotoAlbum: false,
        mediaType: window.navigator.camera.MediaType.ALLMEDIA,
        cameraDirection: window.navigator.camera.Direction.BACK
    };

    buttons.push({
        text: 'Take a photo',
        action: function cameraUpload() {
            // Camera upload
            window.navigator.camera.getPicture(onCameraSuccess, onCameraError, angular.extend({
                sourceType: window.navigator.camera.PictureSourceType.CAMERA
            }, photoConfig));
        }
    });

    buttons.push({
        text: 'Choose a photo',
        action: function deviceUpload() {
            // Device upload
            window.navigator.camera.getPicture(onCameraSuccess, onCameraError, angular.extend({
                sourceType: window.navigator.camera.PictureSourceType.PHOTOLIBRARY
            }, photoConfig));
        }
    });

    $ionicActionSheet.show({
        buttons: buttons,
        cancelText: 'Cancel',
        cancel: function cancel() {},
        buttonClicked: function buttonClicked(index) {
            buttons[index].action();

            return true;
        }
    });
};


来源:https://stackoverflow.com/questions/34784460/cordova-plugin-camera-permission-denial-reading-com-android-providers-media-me

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!