【问题标题】:Phonegap: Can you create 2 writers for a single file?Phonegap:你可以为一个文件创建 2 个写入器吗?
【发布时间】:2014-02-20 20:38:58
【问题描述】:

我最近开始使用 phonegap 在 android 上进行开发。我在学习文件插件 API 时遇到了这个问题,我想知道是否可以为一个文件创建 2 个编写器(在两个不同的页面上)。当我在两个不同的页面上尝试下面的代码时(一个写道:“该团队尚未被球探”,另一个写道:“该团队已被球探:)”出于某种原因,如果我运行,一次只有一个可以工作第一个首先创建文件并写入文件,但第二个将不起作用。同样,如果我先运行第二个,它会创建文件并写入文件,但第一个将不起作用。

<script type="text/javascript" charset="utf-8">
alert("waiting...");
// Wait for device API libraries to load
//
document.addEventListener("deviceready", onDeviceReady, false);

// device APIs are available
//
function onDeviceReady() {
    alert("Device Ready!");
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onRequestFileSystemSuccess, null);
}
function onRequestFileSystemSuccess(fileSystem) {
    alert("Got file system!");
    fileSystem.root.getDirectory("FRC_SCOUT_DATA", { create: true }, onGotDir, null);

}
function onGotDir(dataDir) {
    alert("Got Directoy!");
    dataDir.getFile("data_1539.txt", { create: true, exclusive: true }, onFileCreated, null);
    alert("Got File!");
}
function onFileCreated(dataFile) {
    dataFile.createWriter(gotFileWriter, null);
}
function gotFileWriter(writer) {
    writer.write("This team has been scouted :)"); 
}


</script>

(第二页上的代码基本相同,除了要写入文本文件的消息)

【问题讨论】:

  • 第二页出现什么错误???

标签: android file cordova writer


【解决方案1】:

您保留了exclusive: true。因此,如果文件已存在,请尝试将其设置为false。检查此out

所以从这里更改您的代码

dataDir.getFile("data_1539.txt", { create: true, exclusive: true }, onFileCreated, null);

dataDir.getFile("data_1539.txt", { create: true, exclusive: false}, onFileCreated, fail);

function fail(error) {
    console.log(error.code);
}

【讨论】:

  • 啊,我应该在文档中看到这一点,不过谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-12-21
  • 2021-08-23
  • 1970-01-01
  • 2020-04-04
  • 1970-01-01
  • 2010-11-18
  • 1970-01-01
相关资源
最近更新 更多