2015-09-11 31 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 
} 
相关问题