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

只愿长相守 提交于 2019-12-11 07:27:00

问题


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 ? i m on delphi berlin.


回答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}


来源:https://stackoverflow.com/questions/41201424/how-to-know-the-available-disk-space-on-android-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!