2016-09-21 104 views
1

IAM的努力,當我在電話部署和運行這個函數返回的錯誤,從網址上傳圖片到手機的本地存儲現在離子 - 打開失敗:EACCES(拒絕)

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> 

但錯誤STI會不會改變? 我錯過了什麼嗎?請幫忙!

+0

檢查我的答案,如果這可以解決您的問題,那麼標記它是正確的。 – Abbas

+0

@Abbas我認爲你顯示的是原生android和iam在Ionic(Hybrid App)上工作的代碼。 順便說一句,我發現解決方案,直接給予權限。 – ArsalanK

+0

是的我不知道離子框架,我看到EACCESS併發布了答案,我的不好。我將刪除它。 – Abbas

回答

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

謝謝你指點我PR。我使用的是離子2,最後我發現直接使用uri from fileChooser(無需通過filePath轉換)對我無任何修改。 – Luckylooke

0

這裏是我的離子2版本的@Aldracor破解,如果有人想嘗試。

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

     }); 

    } 
相關問題