在phonegap上使用android编写和读取.txt文件需要帮助
大家好我是phonegap的新手。。 我想在android上使用phonegap创建文件和写入文件,下面的代码不适合我 这是我的文件.html,位于*android\u资产/www/File.html*在phonegap上使用android编写和读取.txt文件需要帮助,android,file-io,cordova,Android,File Io,Cordova,大家好我是phonegap的新手。。 我想在android上使用phonegap创建文件和写入文件,下面的代码不适合我 这是我的文件.html,位于*android\u资产/www/File.html* <!DOCTYPE HTML> <html> <head> <meta name="viewport" content="width=320; user-scalable=no" /> <meta http-
<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=320; user-scalable=no" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>PhoneGap</title>
<link rel="stylesheet" href="master.css" type="text/css" media="screen"
title="no title" charset="utf-8">
<script type="text/javascript" charset="utf-8" src="phonegap.0.9.5.1.js"></script>
<script type="text/javascript" charset="utf-8" src="main.js"></script>
<script type="text/javascript" charset="utf-8">
// Wait for PhoneGap to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// PhoneGap is ready
//
function onDeviceReady() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}
function gotFS(fileSystem) {
fileSystem.root.getFile("test.txt", null, 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(error.code);
}
</script>
</head>
<body>
<h1>Example</h1>
<p>Write File</p>
</body>
</html>
logcat是这样显示的
07-06 13:15:10.732: DEBUG/ddm-heap(356): Got feature list request
07-06 13:15:10.742: DEBUG/dalvikvm(30): GC freed 283 objects / 10848 bytes in 278ms
07-06 13:15:10.922: DEBUG/dalvikvm(30): GC freed 45 objects / 2008 bytes in 160ms
07-06 13:15:11.103: DEBUG/dalvikvm(30): GC freed 2 objects / 48 bytes in 119ms
07-06 13:15:11.593: INFO/System.out(356): Native Geolocation not supported - we're ok
07-06 13:15:11.602: INFO/System.out(356): loadUrl(file:///android_asset/www/File.html)
07-06 13:15:11.622: INFO/System.out(356): url=file:///android_asset/www/File.html baseUrl=file:///android_asset/www
07-06 13:15:11.731: WARN/InputManagerService(51): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@438c1e70 (uid=10001 pid=95)
07-06 13:15:11.792: INFO/ActivityManager(51): Displayed activity android.com.testfile/.testfileActivity: 1429 ms (total 1429 ms)
07-06 13:15:16.982: DEBUG/dalvikvm(217): GC freed 732 objects / 64408 bytes in 85ms
我在AndroidManifest.xml中添加了这个权限
我错过了什么或者这里出了什么问题。。请先帮我谢谢-普拉尼亚我认为PhoneGap的例子遗漏了一些重要的东西。这一行就在这里:
fileSystem.root.getFile("test.txt", null, gotFileEntry, fail);
将仅在文件存在时加载,请尝试按如下方式更新:
fileSystem.root.getFile("test.txt", {create: true}, gotFileEntry, fail);
如果它不存在,它将创建它。调试是怎么说的?您是否放置权限?@subsider plz查看我编辑的代码。。pragna为什么有super.init();第一?您的Web服务器是否支持代理?
fileSystem.root.getFile("test.txt", null, gotFileEntry, fail);
fileSystem.root.getFile("test.txt", {create: true}, gotFileEntry, fail);