From 9ea085745605824d52b5a4c4cf9cbb660eb2f2a2 Mon Sep 17 00:00:00 2001 From: Joe Bowser Date: Wed, 28 Oct 2009 13:43:39 -0700 Subject: [PATCH] Fixed File I/O --- AndroidManifest.xml | 4 +++- assets/www/index.html | 6 ++++++ src/com/phonegap/demo/DroidGap.java | 2 +- src/com/phonegap/demo/FileUtils.java | 23 +++++++++++++++++------ 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 900dbf5b..432917db 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -5,6 +5,7 @@ + @@ -12,6 +13,7 @@ + @@ -28,6 +30,6 @@ - + diff --git a/assets/www/index.html b/assets/www/index.html index cf787db1..b77930ab 100644 --- a/assets/www/index.html +++ b/assets/www/index.html @@ -93,6 +93,11 @@ 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(){ document.addEventListener("touchmove", preventBehavior, false); setTimeout(deviceInfo, 1); @@ -120,6 +125,7 @@ Vibrate Get a Picture Read from file + Write to File
diff --git a/src/com/phonegap/demo/DroidGap.java b/src/com/phonegap/demo/DroidGap.java index f2cfbdf1..09ab728b 100644 --- a/src/com/phonegap/demo/DroidGap.java +++ b/src/com/phonegap/demo/DroidGap.java @@ -100,7 +100,7 @@ public class DroidGap extends Activity { accel = new AccelListener(this, appView); launcher = new CameraLauncher(appView, this); mContacts = new ContactManager(this, appView); - fs = new FileUtils(); + fs = new FileUtils(appView); // This creates the new javascript interfaces for PhoneGap appView.addJavascriptInterface(gap, "DroidGap"); diff --git a/src/com/phonegap/demo/FileUtils.java b/src/com/phonegap/demo/FileUtils.java index b78c76a2..90a01a5d 100644 --- a/src/com/phonegap/demo/FileUtils.java +++ b/src/com/phonegap/demo/FileUtils.java @@ -2,12 +2,21 @@ package com.phonegap.demo; import java.io.*; +import android.webkit.WebView; + public class FileUtils { + + WebView mView; DirectoryManager fileManager; FileReader f_in; FileWriter f_out; + FileUtils(WebView view) + { + mView = view; + } + public int testSaveLocationExists(){ if (fileManager.testSaveLocationExists()) return 0; @@ -87,20 +96,20 @@ public class FileUtils { } catch (IOException e) { data = "FAIL: IO ERROR"; } - + + mView.loadUrl("javascript:navigator.file.hasRead('" + data + "')"); return data; } public int write(String filename, String data) { - { int i=0; String FilePath="/sdcard/" + filename; try { ByteArrayInputStream in = new ByteArrayInputStream(data.getBytes()); byte buff[] = new byte[1024]; FileOutputStream out= - new FileOutputStream(FilePath); + new FileOutputStream(FilePath, true); do { int numread = in.read(buff); if (numread <= 0) @@ -110,9 +119,11 @@ public class FileUtils { i++; } while (true); out.flush(); - out.close(); - } catch (Exception e) { e.printStackTrace(); } - } + out.close(); + mView.loadUrl("javascript:navigator.file.winCallback('File written')"); + } catch (Exception e) { + mView.loadUrl("javascript:navigator.file.failCallback('Fail')"); + } return 0; } }