mirror of
https://github.com/apache/cordova-android.git
synced 2025-01-31 17:32:51 +08:00
Fixed File I/O
This commit is contained in:
parent
e36e4bc0a3
commit
4bb3237732
@ -2,17 +2,16 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
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.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.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.WRITE_EXTERNAL_STORAGE" />
|
||||
|
||||
<application android:icon="@drawable/icon" android:label="@string/app_name"
|
||||
android:debuggable="true">
|
||||
@ -29,6 +28,6 @@
|
||||
<action android:name="android.intent.action.PICK" />
|
||||
</activity>
|
||||
</application>
|
||||
<uses-sdk android:minSdkVersion="3" />
|
||||
<uses-sdk android:minSdkVersion="4" />
|
||||
|
||||
</manifest>
|
@ -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 @@
|
||||
<a href="#" class="btn" onclick="vibrate();">Vibrate</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="writeFile();">Write to File</a>
|
||||
<div id="viewport" class="viewport">
|
||||
<img style="width:60px;height:60px" id="test_img" src="" />
|
||||
</div>
|
||||
|
@ -7,16 +7,8 @@
|
||||
# "build.properties", and override values to adapt the script to your
|
||||
# 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=
|
||||
# Project target.
|
||||
target=Google Inc.:Google APIs:4
|
||||
target=android-4
|
||||
# Indicates whether an apk should be generated for each density.
|
||||
split.density=false
|
||||
|
@ -98,7 +98,7 @@ public class DroidGap extends Activity {
|
||||
geo = new GeoBroker(appView, this);
|
||||
accel = new AccelListener(this, appView);
|
||||
launcher = new CameraLauncher(appView, this);
|
||||
fs = new FileUtils();
|
||||
fs = new FileUtils(appView);
|
||||
|
||||
// This creates the new javascript interfaces for PhoneGap
|
||||
appView.addJavascriptInterface(gap, "DroidGap");
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user