3

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.

nomaam
  • 1,141
  • 2
  • 19
  • 34

6 Answers6

3

mostly for self-reference, there is now an api for this :

cordova.exec(win, fail, "File", "getFreeDiskSpace", []);
RomainMF
  • 66
  • 3
  • This actually seems to work: `cordova.exec(callbackSuccess, callbackFailure, "File", "getFreeDiskSpace", []);` – the_karel May 08 '14 at 16:06
  • does this work in phonegap as well? from what I know cordova and phonegap have actually separated. I tried looking this up and could not find it in the phonegap API documentation. could you post a link to the docs where you found this? thanks, looks very cool and simple ! – nomaam May 08 '14 at 18:48
  • I have created an answer with explanation. – the_karel May 09 '14 at 08:29
3

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)
}
2

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)

Igor.K
  • 155
  • 1
  • 10
the_karel
  • 1,274
  • 11
  • 14
0

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.

Adam Ware
  • 1,182
  • 10
  • 17
0

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.

Melvin Joseph Mani
  • 1,494
  • 15
  • 25
0

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 .

Faizal
  • 702
  • 1
  • 6
  • 23