大家好,我是一个非常新的手机... 我想在android上使用phonegap创建文件和写文件,下面的代码对我不起作用...... i've taken help from here
这是我的 File.html ,位于 * android_asset / www / File.html *
<!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>
这是我的班级
public class testfileActivity extends DroidGap {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
super.init();
super.loadUrl("file:///android_asset/www/File.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
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
我错过了什么,或者这里有什么错误..请提前帮助我 - Pragna
答案 0 :(得分:3)
我认为PhoneGap的例子缺少一些重要的东西。这一行就在这里:
fileSystem.root.getFile("test.txt", null, gotFileEntry, fail);
只会在文件存在时加载它,尝试按如下方式更新它:
fileSystem.root.getFile("test.txt", {create: true}, gotFileEntry, fail);
如果它尚不存在,将创建它。