Fixed File I/O

This commit is contained in:
Joe Bowser 2009-10-28 13:43:39 -07:00
parent e36e4bc0a3
commit 4bb3237732
5 changed files with 31 additions and 23 deletions

View File

@ -2,17 +2,16 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.phonegap.demo" android:versionName="1.0.1" android:versionCode="2"> package="com.phonegap.demo" android:versionName="1.0.1" android:versionCode="2">
<uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <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_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.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" /> <uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application android:icon="@drawable/icon" android:label="@string/app_name" <application android:icon="@drawable/icon" android:label="@string/app_name"
android:debuggable="true"> android:debuggable="true">
@ -29,6 +28,6 @@
<action android:name="android.intent.action.PICK" /> <action android:name="android.intent.action.PICK" />
</activity> </activity>
</application> </application>
<uses-sdk android:minSdkVersion="3" /> <uses-sdk android:minSdkVersion="4" />
</manifest> </manifest>

View File

@ -93,6 +93,11 @@
navigator.file.read('/sdcard/phonegap.txt', fail , fail); navigator.file.read('/sdcard/phonegap.txt', fail , fail);
} }
function writeFile()
{
navigator.file.write('foo.txt', "This is a test of writing to a file", fail, fail);
}
function init(){ function init(){
document.addEventListener("touchmove", preventBehavior, false); document.addEventListener("touchmove", preventBehavior, false);
setTimeout(deviceInfo, 1); setTimeout(deviceInfo, 1);
@ -120,6 +125,7 @@
<a href="#" class="btn" onclick="vibrate();">Vibrate</a> <a href="#" class="btn" onclick="vibrate();">Vibrate</a>
<a href="#" class="btn" onclick="show_pic();">Get a Picture</a> <a href="#" class="btn" onclick="show_pic();">Get a Picture</a>
<a href="#" class="btn" onclick="readFile();">Read from file</a> <a href="#" class="btn" onclick="readFile();">Read from file</a>
<a href="#" class="btn" onclick="writeFile();">Write to File</a>
<div id="viewport" class="viewport"> <div id="viewport" class="viewport">
<img style="width:60px;height:60px" id="test_img" src="" /> <img style="width:60px;height:60px" id="test_img" src="" />
</div> </div>

View File

@ -7,16 +7,8 @@
# "build.properties", and override values to adapt the script to your # "build.properties", and override values to adapt the script to your
# project structure. # project structure.
# apk configurations. This property allows creation of APK files with limited
# resources. For example, if your application contains many locales and
# you wish to release multiple smaller apks instead of a large one, you can
# define configuration to create apks with limited language sets.
# Format is a comma separated list of configuration names. For each
# configuration, a property will declare the resource configurations to
# include. Example:
# apk-configurations=european,northamerica
# apk-config-european=en,fr,it,de,es
# apk-config-northamerica=en,es
apk-configurations= apk-configurations=
# Project target. # Project target.
target=Google Inc.:Google APIs:4 target=android-4
# Indicates whether an apk should be generated for each density.
split.density=false

View File

@ -98,7 +98,7 @@ public class DroidGap extends Activity {
geo = new GeoBroker(appView, this); geo = new GeoBroker(appView, this);
accel = new AccelListener(this, appView); accel = new AccelListener(this, appView);
launcher = new CameraLauncher(appView, this); launcher = new CameraLauncher(appView, this);
fs = new FileUtils(); fs = new FileUtils(appView);
// This creates the new javascript interfaces for PhoneGap // This creates the new javascript interfaces for PhoneGap
appView.addJavascriptInterface(gap, "DroidGap"); appView.addJavascriptInterface(gap, "DroidGap");

View File

@ -2,12 +2,21 @@ package com.phonegap.demo;
import java.io.*; import java.io.*;
import android.webkit.WebView;
public class FileUtils { public class FileUtils {
WebView mView;
DirectoryManager fileManager; DirectoryManager fileManager;
FileReader f_in; FileReader f_in;
FileWriter f_out; FileWriter f_out;
FileUtils(WebView view)
{
mView = view;
}
public int testSaveLocationExists(){ public int testSaveLocationExists(){
if (fileManager.testSaveLocationExists()) if (fileManager.testSaveLocationExists())
return 0; return 0;
@ -87,20 +96,20 @@ public class FileUtils {
} catch (IOException e) { } catch (IOException e) {
data = "FAIL: IO ERROR"; data = "FAIL: IO ERROR";
} }
mView.loadUrl("javascript:navigator.file.hasRead('" + data + "')");
return data; return data;
} }
public int write(String filename, String data) public int write(String filename, String data)
{ {
{
int i=0; int i=0;
String FilePath="/sdcard/" + filename; String FilePath="/sdcard/" + filename;
try { try {
ByteArrayInputStream in = new ByteArrayInputStream(data.getBytes()); ByteArrayInputStream in = new ByteArrayInputStream(data.getBytes());
byte buff[] = new byte[1024]; byte buff[] = new byte[1024];
FileOutputStream out= FileOutputStream out=
new FileOutputStream(FilePath); new FileOutputStream(FilePath, true);
do { do {
int numread = in.read(buff); int numread = in.read(buff);
if (numread <= 0) if (numread <= 0)
@ -110,9 +119,11 @@ public class FileUtils {
i++; i++;
} while (true); } while (true);
out.flush(); out.flush();
out.close(); out.close();
} catch (Exception e) { e.printStackTrace(); } mView.loadUrl("javascript:navigator.file.winCallback('File written')");
} } catch (Exception e) {
mView.loadUrl("javascript:navigator.file.failCallback('Fail')");
}
return 0; return 0;
} }
} }