Skip to content Skip to sidebar Skip to footer

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 p

Solution 1:

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.

functionavailableBytes(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)
}

Solution 2:

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", []);

Solution 3:

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

Post a Comment for "With Phonegap, How Can I Check How Much Free Disk Space Remaining?"