gpt4 book ai didi

javascript - 使用 Phonegap,我如何检查剩余的可用磁盘空间?

转载 作者:行者123 更新时间:2023-11-29 15:43:34 25 4
gpt4 key购买 nike

我有一个 Phonegap 应用程序,它使用文件系统 API 将文件保存在设备文件系统上。

应用启动时需要一些文件系统空间,window.requestFileSystem 但运行时可以下载其他文件,我无法预测所需的磁盘空间总量。

想法是在下载开始前检查可用空间,如果空间不足则触发消息给用户。

Phonegap 是否有一些方法可以检查剩余可用空间?

最佳答案

发现前this post我创建了自己的函数来执行此操作,尝试获取具有确定字节数的文件系统,直到达到限制并递归地占用可用空间。由于回调,也许有必要设置一个大于 0 的超时。在我的测试中,cordova.exec 方式和我的方式之间的差异非常小,并且可能会更好地降低限制值的准确性。

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

可以像这样使用:

availableBytes(function(free_bytes){
alert(free_bytes)
}

关于javascript - 使用 Phonegap,我如何检查剩余的可用磁盘空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15336567/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com