With Phonegap, how can I check how much free disk space remaining?

I have a Phonegap app that uses filesystem API to save file on device filesystem.

On startup the app requires some filesystem space with window.requestFileSystem but runtime it's possible to download other files and I can't predict the total amount of needed disk space.

The idea is to check the space available before download begins and if the space is not enough then trigger a message to user.

Is there some method with Phonegap to check remaining free space?

Phonegap does not provide this feature. The solution is to create yourself a phonegap plugin to check the space available.

Before found 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)
        window.requestFileSystem(LocalFileSystem.PERSISTENT, parseInt(end_temp - ((end_temp - start_temp) / 2)), function(fileSystem){
                availableBytes(callback_temp, parseInt(parseInt(end_temp - ((end_temp - start_temp) / 2))), end_temp)
            }, 0)
        }, function(){
                availableBytes(callback_temp, start_temp, parseInt(end_temp - ((end_temp - start_temp) / 2)))
            }, 0)

Can be used like:


The code will call the getFreeDiskSpace method of the File plugin (cordova.file.plugin) and depending on the success or error callback, give you a result. If the success callback is reached, you’ll get the total free disk space in kilobytes. You can easily convert to megabytes or gigabytes with simple math.

cordova.exec(function(result) {
    alert("Free Disk Space: " + result);
}, function(error) {
    alert("Error: " + error);
}, "File", "getFreeDiskSpace", []);