angularjscordovaionic-frameworkcordova-pluginsngcordova

Cordova file writing throws EACCES (Permission denied) error


I was using cordova-plugin-file and cordova-plugin-file-transfer on Ionic mobile application to download some files to Android device which can open with native applications like pdf, word, excel..etc. This plugin was working perfectly before the Marshmallow os update.

It is now throwing "exception":"/storage/emulated/0/logo_radni.png: open failed: EACCES (Permission denied)"}.

in config.xml file I have also added following permissions as well.

<platform name="android">
   <preference name="AndroidPersistentFileLocation" value="Compatibility" />
   <preference name="AndroidExtraFilesystems" value="files,files-external,documents,sdcard,cache,cache-external,assets,root" />
</platform>

This is working on previous android os version. Here is my code sample

 angular.module('starter.controllers', ['ionic', 'ngCordova'])
 .controller('DashCtrl', function ($scope, $ionicPlatform, $cordovaFileTransfer) {

    $scope.downloadFile = function () {
            $ionicPlatform.ready(function () {

              // File for download
                var url = "http://www.gajotres.net/wp-content/uploads/2015/04/logo_radni.png";

                // File name only
                var filename = url.split("/").pop();

                // Save location
                var targetPath = cordova.file.externalRootDirectory + filename;

                $cordovaFileTransfer.download(url, targetPath, {}, true).then(function (result) {
                    console.log('Success');
                }, function (error) {
                    console.log(JSON.stringify(error));
                }, function (progress) {
                    // PROGRESS HANDLING GOES HERE
                });
            });

        };
    })

Note:

I need to download files into cordova.file.externalRootDirectory to give access to other applications like pdf readers, word, excel..etc

Plugin References:

https://github.com/apache/cordova-plugin-file

https://github.com/apache/cordova-plugin-file-transfer

http://ngcordova.com/docs/plugins/fileTransfer/

Any one have an idea to sort this out?


Solution

  • After the investigation I was able to find the root cause of the issue. This permission issue error is throwing due to the OS Upgrade to Marshmallow. They have changed permission granting procedure. After the API level 23, applications won't get permission at the time of installation. Users have to grant permission at the time of using the Application. So I had to explicitly install a plugin to grant permissions at the time of using device resources.

    I have written a quick blog post on this to share with the community. Also thanks @Beat as well :)

    Anyone can refer the http://anishantha87.blogspot.com/2016/10/read-and-write-permission-for-storage.html for quick sample application.

    You can find Android API Reference for OS Upgrade permissions here.

    Hope this will be helpful for someone else.

    Cheers!