【问题标题】:In Phonegap how to get free space of device在Phonegap中如何获得设备的可用空间
【发布时间】:2015-10-07 05:12:34
【问题描述】:

我正在尝试在 Android、ios 和 Windows 平台上使用 cordova 制作离线在线应用程序。在这个应用程序中有 570MB 的数据存储在数据库中。所以我需要先检查设备的可用内存空间。但我总是得到 0。我正在使用这段代码。给我任何解决方案如何在所有平台上获得可用空间。

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

【问题讨论】:

    标签: cordova memory-management


    【解决方案1】:

    我得到了 iOS 的解决方案。 使用以下代码更新您 phonegap 中的 CDVFile.m:

    - (void)getFreeDiskSpace:(CDVInvokedUrlCommand*)command
    {    
        uint64_t totalSpace = 0;
        uint64_t totalFreeSpace = 0;
        NSError *error = nil;
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error: &error];
    
        if (dictionary) {
            NSNumber *fileSystemSizeInBytes = [dictionary objectForKey: NSFileSystemSize];
            NSNumber *freeFileSystemSizeInBytes = [dictionary objectForKey:NSFileSystemFreeSize];
            totalSpace = [fileSystemSizeInBytes unsignedLongLongValue];
            totalFreeSpace = [freeFileSystemSizeInBytes unsignedLongLongValue];
            NSLog(@"Memory Capacity of %llu MiB with %llu MiB Free memory available.", ((totalSpace/1024ll)/1024ll), ((totalFreeSpace/1024ll)/1024ll));
        } else {
            //NSLog(@"Error Obtaining System Memory Info: Domain = %@, Code = %@", [error domain], [error code]);
            NSLog(@"Error Obtaining System Memory Info");
        }
        NSString* strFreeSpace = [NSString stringWithFormat:@"%llu", totalFreeSpace];
        CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:strFreeSpace];
    
        [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
    
        //return totalFreeSpace;
    
    }
    

    然后在 javascript 文件中使用此代码。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-09
      • 2020-11-08
      • 2016-02-28
      • 2014-07-23
      • 1970-01-01
      • 2013-12-13
      • 2011-07-04
      相关资源
      最近更新 更多