Reputation: 33
I've recently started using phonegap to develop on android. I ran into this problem while learning the File plugin API, I was wondering if it is possible to create 2 writers for a file (on two different pages). When I tried the code below on two different pages (one writes: "This team has not been scouted" and the other writes: "This team has been scouted :)" For some reason only one will work at a time, if i run the first one first it creates the file and writes to it, but the second one won't work. Likewise, if I run the second one first it creates the file and writes to it, but the first one won't work.
<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>
(code on second page is essentially the same with the exception of the message thats being written to the text file)
Upvotes: 0
Views: 48
Reputation: 1455
You have kept exclusive: true
.So it will give you an error if file already exist try setting it to false.Check this out
So change your code from this
dataDir.getFile("data_1539.txt", { create: true, exclusive: true }, onFileCreated, null);
to
dataDir.getFile("data_1539.txt", { create: true, exclusive: false}, onFileCreated, fail);
function fail(error) {
console.log(error.code);
}
Upvotes: 1