最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - PhoneGap - Display available free space? - Stack Overflow

programmeradmin3浏览0评论

I am using PhoneGap and am trying to display to the user the amount of free space available for storage.

I have tried various different types of code to get this to work but without luck.

Does anyone have a working example of how to display free storage space in PhoneGap? I am currently working in android but I'm looking for a phonegap cross platform solution.

Thank you.

I am using PhoneGap and am trying to display to the user the amount of free space available for storage.

I have tried various different types of code to get this to work but without luck.

Does anyone have a working example of how to display free storage space in PhoneGap? I am currently working in android but I'm looking for a phonegap cross platform solution.

Thank you.

Share Improve this question asked Jul 22, 2012 at 23:47 nomaamnomaam 1,2733 gold badges23 silver badges40 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 3

mostly for self-reference, there is now an api for this :

cordova.exec(win, fail, "File", "getFreeDiskSpace", []);

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

As mentioned by @RomainMF, the following works in Cordova 3.4.1-0.1.0:

cordova.exec(
    function(freeSpace) {
      alert('reported free space is ' + freeSpace);
    },
    function() {
      alert('failed');
    }, 
    "File", "getFreeDiskSpace", []
  );

The freeSpace argument is the available storage in bytes or -1 if not available.

It works on a 4.0 AVD and my Galaxy S4, and reports external storage correctly (see below).

Don't forget to run your code after deviceready and cordova plugin add org.apache.cordova.file

Plugin docs: https://github./apache/cordova-plugin-file/

This call is not mentioned in the documentation, but the Java/Android source code is available to read (as well as iOS/Objective-C. AFAIK you can only get the free space of the external storage this way, as the checkInternal parameter is set to false (see getFreeDiskSpace)

I would guess this isn't part of the spec of Phonegap itself rather then a native code solution that you might have to make a phonegap plugin for.

I can't answer for Android but on IOS as the app is sandboxed I'm not sure you would be able to access the amount of free space.

In phone gap you may not be able to get it done, but instead you can create and use a plugin. Click here for more information that may lead you to a solution for your problem.

Phonegap doesn't provide this function, I think.
So you need to to get the available space from SD card by Java, native Android SDK.

Something like this:

File sdcard = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(sdcard.getAbsolutePath());
long available = stat.getAvailableBlocks() * (long) stat.getBlockSize();

Then pass the value via intent into PhoneGap using WebIntent plugin. Now you get the available free space in PhoneGap .

发布评论

评论列表(0)

  1. 暂无评论