writing and reading .txt file using android on phonegap need help
Hello all i am very new to phonegap.. i want to create file and write file using on android using phonegap the below code not working for me... i've taken help from here
this is my File.html that is located into *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>
this is my class
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 shows like this
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
and this permission i've added in 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_CONT开发者_StackOverflowACTS" />
<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" />
what thing i am missing or what wrong this is over here.. please help me thanks in advance - Pragna
I think PhoneGap's example is missing something important. This line right here:
fileSystem.root.getFile("test.txt", null, gotFileEntry, fail);
Will only load it if the file exists, try updating it as follows:
fileSystem.root.getFile("test.txt", {create: true}, gotFileEntry, fail);
Which will create it if it doesn't already exist.
精彩评论