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

二次信任 提交于 2019-12-01 23:40:40

问题


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?


回答1:


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

  • Phonegap plugin development guide
  • android implementation
  • iOS implementation



回答2:


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



回答3:


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


来源:https://stackoverflow.com/questions/15336567/with-phonegap-how-can-i-check-how-much-free-disk-space-remaining

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!