gpt4 book ai didi

android - 在 phonegap 中写入和读取文件

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:13:11 25 4
gpt4 key购买 nike

我尝试在 phonegap+android写入/读取 一个文件,这里是设置:

$(document).ready(function() {
document.addEventListener("deviceready", deviceready, true);

$(document).bind("deviceready", function(){
//writeFile();
//readFile();
});
});

function deviceready() {
writeFile();
readFile();
}

// This is just to do this.
function readFile() {
var d = navigator.file.read('/sdcard/foo.xml', success(), fail());
console.warn(d);
}

function writeFile() {
navigator.file.write('/sdcard/foo.xml', "This is a test of writing to a file",
success(), fail());
}

但在 Android 2.2 的模拟器上,我收到以下错误消息:

08-06 14:21:29.428: INFO/Web Console(936): Error in success callback: Network Status1 = TypeError: Result of expression 'navigator.file' [undefined] is not an object. at file:///android_asset/www/phonegap.0.9.6.js:649

什么可以遗漏,什么可以尝试?

最佳答案

这也适用于 Android 2.2。你调用 load();来自 body 的 onLoad 的函数,以及来自某个按钮的 onClick 的 writeFileFromSDCard(string),将要写入文件的字符串作为参数传递。

<script type="text/javascript" charset="utf-8">

// Event listener
function load() {
document.addEventListener('deviceready', init, false);
}

// Called when device is ready - Do nothing
function init() {
}

// Called to write file to card
function writeFileFromSDCard(param) {

var writer = new FileWriter("/sdcard/write.txt");
writer.write(param + "\n", false);
alert("file Written to SD Card");
}

</script>

关于android - 在 phonegap 中写入和读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6967696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com