Cordova文件写入抛出EACCES(Permission denied)错误

Aru*_*una 3 angularjs cordova ionic-framework cordova-plugins ngcordova

我使用cordova-plugin-filecordova-plugin-file-transfer对离子移动应用下载一些文件,以Android设备可与如PDF,文字,excel..etc本机应用程序打开.这个插件在Marshmallow os更新之前完美运行.

现在正在投掷"exception":"/storage/emulated/0/logo_radni.png: open failed: EACCES (Permission denied)"}.

在config.xml文件中我也添加了以下权限.

<platform name="android">
   <preference name="AndroidPersistentFileLocation" value="Compatibility" />
   <preference name="AndroidExtraFilesystems" value="files,files-external,documents,sdcard,cache,cache-external,assets,root" />
</platform>
Run Code Online (Sandbox Code Playgroud)

这是在以前的Android操作系统版本上工作.这是我的代码示例

 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
                });
            });

        };
    })
Run Code Online (Sandbox Code Playgroud)

注意:

我需要下载文件cordova.file.externalRootDirectory以访问其他应用程序,如pdf reader,word,excel..etc

插件参考:

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

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

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

任何人都有想法解决这个问题?

Aru*_*una 8

经过调查,我找到了问题的根本原因.由于OS升级到Marshmallow,此权限问题错误.他们已经更改了许可授予程序.在API级别23之后,应用程序在安装时将无法获得许可.用户必须在使用应用程序时授予权限.因此,我必须明确安装插件以在使用设备资源时授予权限.

我已经写了一篇关于此的快速博客文章,与社区分享.还要感谢@Beat :)

任何人都可以参考http://anishantha87.blogspot.com/2016/10/read-and-write-permission-for-storage.html进行快速示例应用.

您可以在此处找到适用于操作系统升级的Android API参考.

希望这对其他人有所帮助.

干杯!