Cordova Phonegap文件插件FileWriter cordova.file

时间:2014-10-07 09:12:24

标签: android file cordova filewriter

为什么会这样:

myFile1 = "myReadme.txt";

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);

function gotFS(fileSystem) {
    fileSystem.root.getFile(myFile1, {create: true, exclusive: false}, gotFileEntry, fail);
}

function gotFileEntry(fileEntry) {
    alert("good");                      
}

function fail(error) {
    alert("Error");
}

这不起作用?

myFile2 = cordova.file.externalDataDirectory + "myReadme.txt";

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);

function gotFS(fileSystem) {
    fileSystem.root.getFile(myFile2, {create: true, exclusive: false}, gotFileEntry, fail);
}

function gotFileEntry(fileEntry) {
    alert("good");                      
}

function fail(error) {
    alert("Error");
}

我们不能为Android使用cordova.file.dataDirectory或cordova.file.externalRootDirectory或externalDataDirectory吗?

谢谢

2 个答案:

答案 0 :(得分:0)

我会尝试回答这个问题(但我确定我们可以做得更好吗?

首先,当我将设备插入计算机时,树路径就是这样:

  • MyFile1.txt
  • 所有MyApplication
    • MYFILES
    • MyCache
  • 应用
  • DCIM
  • 媒体
  • 的音乐
  • 的Android
    • 数据
      • 数据(我认为当你没有扎根时我无法访问?)
      • com.MyDomainName.MyAppName
        • 缓存
        • 文件

然后,

  1. 遵循此官方Cordova文件:http://plugins.cordova.io/#/package/org.apache.cordova.file ,我的第一个愿望是用" cordova.file.dataDirectory"存储我的文件和数据。或" cordova.file.externalDataDirectory"还是别的......?

  2. 当您执行console.log(cordova.file.dataDirectory)时,您获得file:///data/data/com.MyDomainName.MyAppName/files/ =>这样,当您没有root时,就无法访​​问 当你获得console.log(cordova.file.externalDataDirectory)时,你获得了 file:///storage/emulated/0/

  3. 当你做那件事

    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
    function gotFS(fileSystem) { console.log(fileSystem.root) ...
    

    你可以在这个对象中看到:nativeURL:" file:/// storage / emulated / 0 /" ......所以,它和#34; externalDataDirectory"相同。我想我们不能使用" dataDirectory" ...

    无论如何,我不知道这是否是最好的做法,但在这里你是我的解决方案:

    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFSWin, getDirectoryFail);
    
    function onFSWin(fileSystem) {
        fileSystem.root.getDirectory("myFolder", {create: true, exclusive: false}, getDirectorySuccess, getDirectoryFail);
    }
    
    function getDirectorySuccess(parent) {
        console.log("New Folder or Folder already exists: " + parent.fullPath);
    }
    
    function getDirectoryFail(error) {
        console.log("Unable to create new directory: " + error.code);
    }
    
    // Second, we gonna check or create a new file in a specific folder
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFSWin2, onFSFail2);
    
    function onFSWin2(fileSystem) {
        fileSystem.root.getFile("myFolder/myFile1.txt", {create: true, exclusive: false}, gotFileEntry, onFSFail2);
    }
    
    function gotFileEntry(fileEntry) {
        console.log("New file: " + fileEntry.fullPath);
        fileEntry.createWriter(gotFileWriter, onFSFail2);
    }
    
    function gotFileWriter(writer) {
    
    writer.onwriteend = function(evt) {
       console.log("contents of file now 'some sample text'");
    
       writer.truncate(11);
    
       writer.onwriteend = function(evt) {
           console.log("contents of file now 'some sample'");
    
           writer.seek(4);
           writer.write(" different text");
    
           writer.onwriteend = function(evt){
               console.log("contents of file now 'some different text'");
           }
       };
    };
    writer.write("some sample text");
    }
    
    function onFSFail2(error) {
    console.log(error.code);
    }
    

答案 1 :(得分:0)

您可以使用resolveLocalFileSystemURL来使用cordova.file.*属性:

window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, function (dirEntry) {
    dirEntry.getFile("myFile1.txt", {
        create: true,
        exclusive: false
    }, function (fileEntry) {
        // fileEntry.createWriter
    }, onError);
});