简体   繁体   中英

How to know the available disk space on android/ios?

before to save a file to the cache directory i want to know how much space are available on the device (especially the space available where the cache directory is located). How to do this on android and ios ? im on delphi berlin.

ok, here is the solution for all plateform (taken from https://github.com/Zeus64/alcinoe ) :

  {$IFDEF ANDROID}
  aStatFS := TJStatFs.JavaClass.init(StringToJstring(aDir));
  if (TJBuild_VERSION.JavaClass.SDK_INT < 18 {Android 4.3 - JELLY_BEAN_MR2}) then aTmpAvailableSpace := aStatFS.getBlockSize * aStatFS.getAvailableBlocks
  else aTmpAvailableSpace := aStatFS.getAvailableBytes;
  aStatFS := nil;
  {$ELSEIF defined(IOS) or defined(_MACOS)}
  aFileManager := TNSFileManager.Wrap(TNSFileManager.OCClass.defaultManager);
  aDict := aFileManager.attributesOfFileSystemForPath(StrToNSStr(aDir), nil);
  if aDict = nil then aTmpAvailableSpace := 0
  else begin
    aPointer := aDict.objectForKey((CocoaNSStringConst(FoundationFwk, 'NSFileSystemFreeSize') as ILocalObject).GetObjectID);
    if Assigned(aPointer) then aTmpAvailableSpace := TNSNumber.Wrap(aPointer).unsignedLongLongValue
    else aTmpAvailableSpace := 0;
  end;
  {$ELSEIF defined(MSWINDOWS)}
  aDiskDrive := ALupperCaseU(AlStringReplaceU(ExtractFileDrive(aDir), ':', '', []));
  if length(aDiskDrive) = 1 then aTmpAvailableSpace := DiskFree(ord(aDiskDrive[low(aDiskDrive)]) - $40)
  else aTmpAvailableSpace := 0;
  {$ENDIF}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM