如何在Kitkat设备上使用cordova.file插件在SD卡上写文件?

时间:2015-01-07 21:53:12

标签: cordova

我试图用samsung galaxy tab 4设备将文件写入sd卡上的app目录(Kitkat 4.4.2,Cordova 4.1.2,Cordova.file 1.3.2,Android 3.6.4)

如何修改此FileWriter example以写入SD卡上的app目录而不是内部存储器?

另外,如果我尝试console.log(cordova.file.externalDataDirectory);,结果就是

  

file:///storage/emulated/0/Android/data/app.name/files/" on the internal memory and not the Sd Card

我的SD卡可供ES Explorer等其他应用使用。

<!DOCTYPE html>
<html>
  <head>
    <title>FileWriter Example</title>

    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <script type="text/javascript" charset="utf-8">

    // Wait for device API libraries to load
    //
    document.addEventListener("deviceready", onDeviceReady, false);

    // device APIs are available
    //
    function onDeviceReady() {
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
    }

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

    function gotFileEntry(fileEntry) {
        fileEntry.createWriter(gotFileWriter, fail);
    }

    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 fail(error) {
        console.log(error.code);
    }

    </script>
  </head>
  <body>
    <h1>Example</h1>
    <p>Write File</p>
  </body>
</html>

0 个答案:

没有答案