File IO getFreeDiskSpace()

This commit is contained in:
Ray Vahey 2009-03-09 15:55:41 +07:00
parent dfa2f0a802
commit 49844b4e39
4 changed files with 64 additions and 30 deletions

View File

@ -310,8 +310,8 @@ var Device = {
file: {
result: "",
testSaveLocationExists: function(){
Device.file.result = window.DroidGap.testSaveLocationExists();
getFreeDiskSpace: function(){
Device.file.result = window.DroidGap.getFreeDiskSpace();
return Device.file.result;
},
testFileExists: function(file){

View File

@ -1,5 +1,6 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
@ -110,8 +111,8 @@ http = function(func)
}
fileManagement = function(x){
if (x == 'testSDCard'){
$('file_status').value = Device.file.testSaveLocationExists();
if (x == 'getFreeDiskSpace'){
$('file_status').value = Device.file.getFreeDiskSpace();
}else
if (x == 'testFileExists'){
$('file_status').value = Device.file.testFileExists($('checkfile').value);
@ -175,7 +176,9 @@ phInformation = function(){
addLoadEvent(initGap);
</script>
</head><body orient="landscape">
</head>
<body orient="landscape">
<div class="toolbar">
<h1 id="pageTitle">Main</h1>
<a style="display: none;" id="backButton" class="button" href="#"></a>
@ -352,33 +355,34 @@ addLoadEvent(initGap);
<input disabled="enabled" name="file_status" id="file_status" value="" type="text"></input>
</div>
<div class="row">
<a class="button leftButton" type="submit" onclick="fileManagement('testSDCard');">testSaveLocation</a>
<a class="button leftButton" type="submit" onclick="fileManagement('getFreeDiskSpace');">getFreeDiskSpace</a>
</div>
<div class="row">
<a class="button leftButton" type="submit" onclick="fileManagement('testFileExists')">testFileExists</a>
<input type=text name="checkfile" id="checkfile" size=60 maxlength=2048 value="" style="width: 30ex"></input>
<input type=text name="checkfile" id="checkfile" value="/hullophonegap.mp3" size=60 maxlength=2048 value="" style="width: 30ex"></input>
</div>
<div class="row">
<a class="button leftButton" type="submit" onclick="fileManagement('testDirectoryExists')">testDirExists</a>
<input type=text name="checkdir" id="checkdir" size=60 maxlength=2048 value="" style="width: 30ex"></input>
<input type=text name="checkdir" id="checkdir" value="/foo" size=60 maxlength=2048 value="" style="width: 30ex"></input>
</div>
<div class="row">
<a class="button leftButton" type="submit" onclick="fileManagement('createDir');">Create Directory</a>
<input type=text name="createfile" id="createfile" size=60 maxlength=2048 value="" style="width: 30ex"></input>
</div>
<div class="row">
<a class="button leftButton" type="submit" onclick="fileManagement('delFile');">Delete File</a>
<input type=text name="delfile" id="delfile" size=60 maxlength=2048 value="" style="width: 30ex"></input>
</div>
<div class="row">
<input type=text name="deldir" id="deldir" size=60 maxlength=2048 value="" style="width: 30ex"></input>
<a class="button leftButton" type="submit" onclick="fileManagement('delDir');">Delete Directory</a>
</div>
</fieldset>
</div>
<div class="row">
<a class="button leftButton" type="submit" onclick="fileManagement('createDir');">Create Directory</a>
<input type=text name="createfile" id="createfile" value="/foo" size=60 maxlength=2048 value="" style="width: 30ex"></input>
</div>
<div class="row">
<a class="button leftButton" type="submit" onclick="fileManagement('delFile');">Delete File</a>
<input type=text name="delfile" id="delfile" value="/hullophonegap.mp3" size=60 maxlength=2048 value="" style="width: 30ex"></input>
</div>
<div class="row">
<input type=text name="deldir" id="deldir" value="/foo" size=60 maxlength=2048 value="" style="width: 30ex"></input>
<a class="button leftButton" type="submit" onclick="fileManagement('delDir');">Delete Directory</a>
</div>
</fieldset>
</div>
<div id="audio" title="audio" class="panel">
<h2>Audio</h2>
<p><i>Make sure your Android sdk sdcard is mounted!</i></p>
<fieldset>
<div class="row">
<label>AudioFile:</label>
@ -408,9 +412,10 @@ addLoadEvent(initGap);
<a class="button leftButton" type="submit" onclick="audio('getAudioOutputDevice');">getAudioDevice</a>
<input disabled="enabled" id="audoutput" type="text" style="width: 30ex"/>
</div>
</fieldset>
</div>
</fieldset>
</div>
<div id="pi" title="Phone Information" class="panel">
<fieldset>
<div class="row">
@ -436,6 +441,9 @@ addLoadEvent(initGap);
</fieldset>
</div>
<div id="preloader"></div></body></html>
<div id="preloader"></div>
</body>
</html>

View File

@ -3,6 +3,7 @@ package com.nitobi.phonegap;
import java.io.File;
import android.os.Environment;
import android.os.StatFs;
public class DirectoryManager {
@ -19,6 +20,25 @@ public class DirectoryManager {
return status;
}
protected long getFreeDiskSpace(){
/*
* gets the available SD card free space or returns -1 if the SD card is not mounted.
*/
String status = Environment.getExternalStorageState();
long freeSpace = 0;
if (status.equals(Environment.MEDIA_MOUNTED)) {
try {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
freeSpace = availableBlocks*blockSize/1024;
} catch (Exception e) {e.printStackTrace(); }
} else { return -1; }
return (freeSpace);
}
protected boolean createDirectory(String directoryName){
boolean status;
if ((testSaveLocationExists())&&(!directoryName.equals(""))){
@ -98,8 +118,8 @@ public class DirectoryManager {
}else
status = false;
return status;
}
private File constructFilePaths (String file1, String file2){
File newPath;
newPath = new File(file1+"/"+file2);

View File

@ -259,6 +259,12 @@ public class PhoneGap{
else
return 1;
}
public long getFreeDiskSpace(){
System.out.println("FOOOOOOOO");
long freeDiskSpace=fileManager.getFreeDiskSpace();
return freeDiskSpace;
}
public int testFileExists(String file){
if (fileManager.testFileExists(file))