Phone Gap: Writing to Files - Can't Get Example To Work
I'm trying to follow the phonegap example file writing code and running into issues.
I have tried running this both on a 2.2 emulator instance and on a phone:
here is my code which is basically a near verbatim paste of the write file code from API docs + jsconsole and console messages:
<script type="text/javascript" src="http://jsconsole.com/remote.js?
[key_ommited]"></script>
<script> console.log('in between loads.'); </script>
<script type="text/javascript" src="phonegap-1.0.0.js"></script>
<script> console.log('after-phonegap.'); </script>
<script type="text/javascript">
console.log('test to see if this fires within new script tag');
//
// // Wait for PhoneGap to load
// //
// console.log('before add event listener!!');
//
document.addEventListener("deviceready", onDeviceReady,
false);
console.log('after event listener declaration');
function onDeviceReady() {
console.log('in onDeviceReady');
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS,
fail);fu开发者_运维百科nction fail);(args) {
}
var test_file_full_path = 'mnt/sdcard/test_file2.txt'
function gotFS(fileSystem) {
fileSystem.root.getFile(test_file_full_path, { create:
true }, gotFileEntry, fail);
}
function gotFileEntry(fileEntry) {
fileEntry.createWriter(gotFileWriter, fail);
}
function gotFileWriter(writer) {
writer.onwrite = function(evt) {
console.log("write success");
};
writer.write("some sample text");
// contents of file now 'some sample text'
writer.truncate(11);
// contents of file now 'some sample'
writer.seek(4);
// contents of file still 'some sample' but file pointer
is after the 'e' in 'some'
writer.write("different text");
// contents of file now 'some different text'
}
function fail(error) {
console.log('in failure: ');
console.log(error.code);
}
And The first time i run the code, What i get from the jsconsole output (oldest messages at bottom) is:
"write success"
"Error in success callback: File4 = 7"
"This is a file"
"in onDeviceReady"
"test to see if this fires within new script tag"
"after-phonegap."
"after event listener declaration"
"in between loads."
The subsequent times that I run the same code, I instead get:
"9"
"in failure: "
"in onDeviceReady"
[rest of messages are the same]
If i go into the DDMS perspective, I see teh generated 'test_file2' file in the directory, but if I try to pull the generated 'test_file2' file, it won't let me pull it off the emulator and says:
[2011-09-30 14:47:32] Failed to pull selection
[2011-09-30 14:47:32] (null)
I also cannot delete this file.
In an earlier attempt i was able to pull the file and see that the first line of the write code 'some sample text' had been written, but presumably it had stopped short of the other write lines.
Anyone have any idea what I am doing wrong / any advice? This writing to file is bothering me in how much trouble i'm having.
精彩评论