Search code examples
androidionic-frameworkngcordova

Permission Denial when copy file from android gallery with ngCordova


I'm using the ionicframework with the ngCordova FileTransfer plugin to copy an image from android gallery.

I'm try the following

$scope.getImage = function()
        {
            var options = {
                quality: 50,
                destinationType: Camera.DestinationType.FILE_URI,
                sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
                saveToPhotoAlbum: false
            };

            $cordovaCamera.getPicture(options).then(
                function(fileUri)
                {
                    console.log(fileUri);
                    $cordovaFileTransfer.download(fileUri, cordova.file.dataDirectory + 'my-image.jpg', {}, true).then(
                        function(fileEntry)
                        {
                            $scope.images.imageUri = fileEntry.nativeURL;
                        },
                        function (error)
                        {
                            console.log(error);
                        }
                    );
                },
                function(error)
                {
                    console.log(error);
                }
            );
        }

In the code above, errocallback is call and console show the error message:

"Permission Denial: reading com.android.providers.media.MediaDocumentsProvider uri content://com.android.providers.media.documents/document/image%253A19753 from pid=3933, uid=10194 requires android.permission.MANAGE_DOCUMENTS, or grantUriPermission()"


Solution

  • $cordovaFileTransfer is not designed for copy/move a file. It is used for download and upload features.

    You have to use $cordovaFile with method copyFile(path, file, newPath, newFile) instead.

    Before using $cordovaFile you have to install its plugin with this command: cordova plugin add org.apache.cordova.file

    More information at ngCordova - $cordovaFile

    Hope it'll help!