Reputation: 1263
I am using PhoneGap and am trying to display to the user the amount of free space available for storage.
I have tried various different types of code to get this to work but without luck.
Does anyone have a working example of how to display free storage space in PhoneGap? I am currently working in android but I'm looking for a phonegap cross platform solution.
Thank you.
Upvotes: 3
Views: 4468
Reputation: 1434
As mentioned by @RomainMF, the following works in Cordova 3.4.1-0.1.0:
cordova.exec(
function(freeSpace) {
alert('reported free space is ' + freeSpace);
},
function() {
alert('failed');
},
"File", "getFreeDiskSpace", []
);
The freeSpace argument is the available storage in bytes or -1 if not available.
It works on a 4.0 AVD and my Galaxy S4, and reports external storage correctly (see below).
Don't forget to run your code after deviceready
and cordova plugin add org.apache.cordova.file
Plugin docs: https://github.com/apache/cordova-plugin-file/
This call is not mentioned in the documentation, but the Java/Android source code is available to read (as well as iOS/Objective-C. AFAIK you can only get the free space of the external storage this way, as the checkInternal parameter is set to false (see getFreeDiskSpace)
Upvotes: 2
Reputation: 77
Before founding this post I create my own function that do this, trying to get a filesystem with a determined bytes until reach the limit and assume the free space, recursively. Due the callbacks, maybe it's necessary set a timeout bigger than 0. In my tests, the diference between the cordova.exec way and my way was very small, and is possible better the accuracy lowering the value of the limit.
function availableBytes(callback, start, end){
callback = callback == null ? function(){} : callback
start = start == null ? 0 : start
end = end == null ? 1 * 1024 * 1024 * 1024 * 1024 : end //starting with 1 TB
limit = 1024 // precision of 1kb
start_temp = start
end_temp = end
callback_temp = callback
if (end - start < limit)
callback(start)
else{
window.requestFileSystem(LocalFileSystem.PERSISTENT, parseInt(end_temp - ((end_temp - start_temp) / 2)), function(fileSystem){
setTimeout(function(){
availableBytes(callback_temp, parseInt(parseInt(end_temp - ((end_temp - start_temp) / 2))), end_temp)
}, 0)
}, function(){
setTimeout(function(){
availableBytes(callback_temp, start_temp, parseInt(end_temp - ((end_temp - start_temp) / 2)))
}, 0)
})
}
}
Can be used like:
availableBytes(function(free_bytes){
alert(free_bytes)
}
Upvotes: 3
Reputation: 66
mostly for self-reference, there is now an api for this :
cordova.exec(win, fail, "File", "getFreeDiskSpace", []);
Upvotes: 3
Reputation: 747
Phonegap doesn't provide this function, I think.
So you need to to get the available space from SD card by Java, native Android SDK.
Something like this:
File sdcard = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(sdcard.getAbsolutePath());
long available = stat.getAvailableBlocks() * (long) stat.getBlockSize();
Then pass the value via intent into PhoneGap using WebIntent plugin. Now you get the available free space in PhoneGap .
Upvotes: 0
Reputation: 1496
In phone gap you may not be able to get it done, but instead you can create and use a plugin. Click here for more information that may lead you to a solution for your problem.
Upvotes: 0
Reputation: 1202
I would guess this isn't part of the spec of Phonegap itself rather then a native code solution that you might have to make a phonegap plugin for.
I can't answer for Android but on IOS as the app is sandboxed I'm not sure you would be able to access the amount of free space.
Upvotes: 0