离子 - 开放失败:EACCES(权限被拒绝)

时间:2016-09-21 10:23:11

标签: android angularjs ionic-framework cordova-plugins ngcordova

我尝试将图片从网址上传到手机的本地存储空间

 appController.controller('TestCtrl',['$scope','$cordovaFileTransfer', function($scope,$cordovaFileTransfer){

$scope.Download = function () {
  ionic.Platform.ready(function(){
         var url = "http://3.bp.blogspot.com/-XchURXRz-5c/U5ApPOrPM9I/AAAAAAAADoo/YZEj4qeSlqo/s1600/Final-Fantasy-XV-Noctis-Red-Eyes.png";
         var filename = url.split("/").pop();
         var targetPath = cordova.file.externalRootDirectory + 'Pictures/' + filename;
         console.log(targetPath);

          $cordovaFileTransfer.download(url, targetPath, {}, true).then(function (result) {
                $scope.hasil = 'Save file on '+targetPath+' success!';
                $scope.mywallpaper=targetPath;
          }, function (error) {
              console.log(error);
                $scope.hasil = 'Error Download file'+error;
          }, function (progress) {
                $scope.downloadProgress = (progress.loaded / progress.total) * 100;
          });
  });
}
}]);

现在,当我在手机中部署并运行此功能时,它会返回错误,通过调试我发现:打开失败:EACCES(权限被拒绝)

我还在AndroidManifest.xml上添加了权限

<manifest android:hardwareAccelerated="true" android:versionCode="1" android:versionName="0.0.1" package="com.foodhubb" xmlns:android="http://schemas.android.com/apk/res/android">
    <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application android:hardwareAccelerated="true" android:icon="@drawable/icon" android:label="@string/app_name" android:supportsRtl="true">
        <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:theme="@android:style/Theme.DeviceDefault.NoActionBar" android:windowSoftInputMode="adjustResize">
            <intent-filter android:label="@string/launcher_name">
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="23" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
</manifest>

但是错误仍然没有改变? 我错过了什么?请帮忙!

2 个答案:

答案 0 :(得分:1)

你是如何解决这个问题的?

目前CordovaFileTransfer不支持这些权限(There is currently a PR open for it on GitHub

我使用$cordovaFile(实现这些权限)同时解决了我的问题:

//HACK - $cordovaFileTransfer doesn't support permissions so we get permission here with $cordovaFile
$cordovaFile.createFile(externalAppPath, "permissions.dat", true)
.then(function (success) {
    $cordovaFile.removeFile(externalAppPath, "permissions.dat")
    .then(function (success) {
        // success
        downloadPromise = $cordovaFileTransfer.download(url, targetPath, options, trustHosts);
        downloadPromise.then(function (result) {
        ...
        ...
    }, function (error) {
        // error couldn't delete tmp file
    });
}, function (error) {
    $scope.modal.hide();
    $ionicPopup.alert({
        title: "Permission denied",
        template: "The file could not be downloaded, permission was denied.",
        cssClass: 'error-popup'
    });
    return;
});

答案 1 :(得分:0)

如果有人想尝试,这是@Aldracor hack的离子2版本。

import { Platform } from 'ionic-angular';
import { Transfer } from '@ionic-native/transfer';
import { File } from '@ionic-native/file';

constructor(
        private transfer: Transfer,
        public platform: Platform,
        private file: File,
    ) {}

    ngOnInit() {

        this.platform.ready().then( () => {
            this.file.createFile(this.file.externalApplicationStorageDirectory, "permissions.dat", true)
                .then(() => {
                    this.file.removeFile(this.file.externalApplicationStorageDirectory, "permissions.dat")
                        .then( () => console.log('success'), error => console.error(error) );
                }, error => console.error(error))

        });

    }