2015-10-07 12 views
0

我想在Android,ios和Windows平臺上使用cordova進行離線在線應用程序。在這個應用程序中有570MB數據要存儲在數據庫中。所以我需要首先檢查設備的可用內存空間。但我總是0.我正在使用這個代碼。給我任何解決方案,我如何在所有平臺獲得免費空間。在Phonegap中如何獲得設備的可用空間

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

回答

3

我得到了iOS的解決方案。 使用此代碼更新您的CDVFile.m在你的PhoneGap:

- (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", [] 
); 
相關問題