【问题标题】:Is there a way to know specifically the root free space (Android and iOS), using Apache Cordova?有没有办法使用 Apache Cordova 专门了解根可用空间(Android 和 iOS)?
【发布时间】:2017-11-13 18:34:02
【问题描述】:

最近我在实际工作中一直在研究这个主题,所有研究都集中在这个 availableBytes 函数上,您可以在下面看到:

function onInitFs(fs) {
    var appDirectory = fs.root;
    window.alert("Directorio de trabajo: " + appDirectory.toURL());
    availableBytes(function (free_bytes) {
    console.log("Interal free space(root + internal): " + (free_bytes / (1024 * 1024 * 1024)).toFixed(0) + "GB");
 });
}
//Function used to know available space in internal storage
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)
     })
 }
}

如您所见,我们可以很容易地知道内部存储空间大小,但根目录(即我存储下载的视频和音乐的位置,客户端不会轻易访问的位置)不能被“询问”,所以...有人知道这样做的正确方法吗?

【问题讨论】:

  • 不知道它是否有帮助,但是当您尝试保存文件并且没有剩余空间时,请求将失败,错误代码为 10 (QUOTA EXCEEDED_ERR)

标签: javascript android ios html cordova


【解决方案1】:

为了获得设备的可用空间,您必须添加cordova-file-plugin:https://github.com/apache/cordova-plugin-file/

然后在您的设备就绪事件之后,您可以运行以下代码:

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

这将以千字节为单位提醒总空间,只需转换为兆字节或千兆字节。

【讨论】:

  • 这是一个很好的答案,但此功能提供的可用空间是内部存储,而不是我要存储应用程序下载内容的根存储。我一直在想的是按照 Akis 所说的那样实现:* 首先,我将尝试“写入”一个大文件,如果它在编写时出错,我将递归调用相同的函数,直到它给出 OK代码,这将是该特定存储空间的可用空间。
  • @DavidTelegimTv 是的,你是对的,我还发现了一个插件,它说它可以做你想做的事,但我从未使用过它,它是这个 github.com/sqli/sqli-cordova-disk-space-plugin。如果你有时间,请检查它
猜你喜欢
  • 1970-01-01
  • 2021-12-08
  • 2021-05-17
  • 2014-07-19
  • 1970-01-01
  • 1970-01-01
  • 2017-03-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多