Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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}

Post a Comment for "How To Know The Available Disk Space On Android/ios?"