电话差距:写入文件 - 无法让示例工作
我正在尝试按照phonegap示例文件编写代码并遇到 问题。
我尝试在 2.2 模拟器实例和 电话:
这是我的代码,基本上是近乎逐字粘贴的写入内容 来自 API 文档 + jsconsole 和控制台消息的文件代码:
<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);function 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);
}
第一次运行代码时,我从 jsconsole 得到什么 输出(最旧的消息在底部)是:
"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."
随后我运行相同的代码,我得到:
"9"
"in failure: "
"in onDeviceReady"
[rest of messages are the same]
如果我进入 DDMS 视角,我会看到生成的“test_file2” 文件在目录中,但如果我尝试提取生成的“test_file2” 文件,它不会让我把它从模拟器上拉下来,并说:
[2011-09-30 14:47:32] Failed to pull selection
[2011-09-30 14:47:32] (null)
我也无法删除这个文件。
在之前的尝试中,我能够提取文件并看到 写入代码的第一行“一些示例文本”已被写入,但是 据推测,它已经在其他写入行之前停止了。
有人知道我做错了什么/有什么建议吗?这种写入文件的方式让我很困扰,因为我遇到了很多麻烦。
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);function 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论