2015-09-11 33 views
6

不知何故,當試圖設置以下權限時,我總是收到「錯誤代碼5」。 我想要做的是將現有的文件從Android中的資產複製到Android設備上的可訪問位置,以便能夠跨其他應用程序(如郵件)共享它。Cordova:無法使用Cordova在Android上覆制文件

這裏是我的代碼示例:

window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem; 
    var storagefolder = cordova.file.dataDirectory; 
    var storagefolderpointer; 
    console.log("storage folder: " + storagefolder); 

    // Check for support. 
    if (window.requestFileSystem) { 
     console.log("filesystem beschikbaar"); 
     var getFSfail = function() { 
      console.log('Could not open filesystem'); 
     }; 
     var getFSsuccess = function(fs) { 

      var getDIRsuccess = function (dir) { 
        console.debug('Got dirhandle'); 
        cachedir = dir; 
        fileurl = fs.root.fullPath + '/' + storagefolder; 
        storagefolderpointer = dir; 
      }; 
      var getDIRfail = function() { 
       console.log('Could not open directory'); 
      }; 

      console.debug('Got fshandle'); 
      FS = fs; 
      FS.root.getDirectory(storagefolder, {create:true,exclusive:false}, getDIRsuccess, getDIRfail); 
     }; 
     window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, getFSsuccess, getFSfail); 

     setTimeout(function() { 

      console.log("directory beschikbaar"); 
      var suc = function(entry){ 
       var goe = function(){ 
        console.log("copy success"); 
       }; 
       var fou = function(){ 
        console.log("copy NOT NOT success"); 
       }; 
       entry.copyTo(storagefolder, "vcard.vcf", goe, fou); 
      }; 
      var fai = function(e){ 
       console.log("fail getFile: " + e.code); 
      }; 
      window.resolveLocalFileSystemURL(storagefolderpointer + "www/visitekaart/vcard.vcf", suc, fai); 

     }, 1000); 

    } else { 
     console.log("filesystem NOT NOT NOT available"); 
    } 
+2

與[此問題](https://github.com/driftyco/ng-cordova/issues/506)有關?請參閱第10條評論 – lifeisfoo

+0

您是否嘗試過使用filechooser? https://github.com/MaginSoft/MFileChooser –

+1

錯誤是'ENCODING_ERR',如文檔https://github.com/apache/cordova-plugin-file所示,請在可能相關的問題中說明它。 –

回答

3

有你使用cordovaFile插件相反,你可以使用BLOB來讀取文件的內容比使用cordovaFile插件

寫在Android上的SD卡一個新的
$cordovaFile.writeFile('appdata/file.txt', blob, 0).then(function(fileEntry) { 
    //success 
}, function(err) { 
    //err 
} 
相關問題