Search code examples
androiddelphifreespace

Delphi Tokyo Android: How do I get the Free Available Disk Space?


I've found this information here, but I seem to be unable to get it working in my application.

Tried to access it using TJMtpStorageInfo.JavaClass.getFreeSpace; but that does not seem to work.

Found another link here, but they don't include where TJALStatFs comes from or the uses clause. TJALStatFs is undefined.

I'm just stuck on figuring this one out. Does anyone have source code they can provide for Delphi Tokyo Android programming so that I can get the available free space on the Android Device?

I'm debugging on Moto G4 Play Android version 6.0.1


Solution

  • Delphi Tokyo 10.2.2 version w/ Android 6.0.1:

    Found this link android.os.StatFs which i need to include into my project.

    My simple program I was designing would fill all the space on android with random data.

    Here is the source code I used to do this. Warning: Code may still be buggy, but currently seems to be doing what it's suppose to. The main objective here was to show full source code for which I got it working. I personally hate it when people give partial source code and you've got to figure out all the rest.

    Thread's don't like to update memo pad, or any of the GUI.. so I commented them out. I've got to create a special TThread annoymous procedure, but currently I was working just to get the application to function. Please forgive me, it's a bit sloppy coding.

    unit Unit1;
    
    interface
    
    uses
      System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
      FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
      IdBaseComponent, IdThreadComponent, FMX.StdCtrls, FMX.Layouts,
      FMX.Controls.Presentation, FMX.ScrollBox, FMX.Memo, System.IOUtils,
      AndroidAPI.JNIBridge,
      Androidapi.JNI.JavaTypes,
      android.os.StatFs,
      Posix.Unistd;
    
    type
      TForm1 = class(TForm)
        Memo1: TMemo;
        Button1: TButton;
        Layout1: TLayout;
        AniIndicator1: TAniIndicator;
        Layout2: TLayout;
        STOP: TButton;
        Layout3: TLayout;
        Label1: TLabel;
        IdThreadComponent1: TIdThreadComponent;
        Button2: TButton;
        procedure IdThreadComponent1Run(Sender: TIdThreadComponent);
        procedure Button1Click(Sender: TObject);
        procedure STOPClick(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure IdThreadComponent1Stopped(Sender: TIdThreadComponent);
        procedure IdThreadComponent1Terminate(Sender: TIdThreadComponent);
      private
        { Private declarations }
      public
        { Public declarations }
        breakit: boolean;
        //createnewrandfileActive: boolean;
        copydataActive: boolean;
        procedure CopyData();
        procedure CreateNewRandFile(Fsize : Int64);
        function CheckDiskSize(aDir : String): Int64;
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.fmx}
    
    uses Androidapi.Helpers;
    
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      breakit := false;
      IdThreadComponent1.Start;
    end;
    
    function TForm1.CheckDiskSize(aDir : String): Int64;
    var aStatFS : JStatFs;
        //aTmpAvailableSpace : Int64;
    begin
      aStatFS := TJStatFs.JavaClass.init(StringToJString(aDir));
      //aTmpAvailableSpace := aStatFS.getBlockSize * aStatFS.getAvailableBlocks;
      //aTmpAvailableSpace := aStatFS.getAvailableBytes;
      //aStatFS := nil;
      result := aStatFS.getAvailableBytes;
    end;
    
    procedure TForm1.CreateNewRandFile(Fsize : Int64);
    var
      FileStream1: TFileStream;
      RandomFileToCopy : string;
      Rand1 : Int64;
    begin
      //createnewrandfileActive := true;
      //memo1.BeginUpdate;
      //memo1.Lines.Add('Begin CreateNewRandFile');
      //memo1.EndUpdate;
      // create random file
      if Fsize > CheckDiskSize(TPath.GetTempPath) then Fsize := CheckDiskSize(TPath.GetTempPath);
    
      //memo1.lines.add('free space: ' + IntToStr(CheckDiskSize((TPath.GetTempPath))));
      //memo1.lines.add('create file size: ' + IntToStr(Fsize));
      Randomize;
      RandomFileToCopy := TPath.GetTempPath + TPath.DirectorySeparatorChar + 'random.fil';
      //memo1.Lines.Add(RandomFileToCopy);
      if FileExists(RandomFileToCopy) = false then
      begin
        FileStream1 := TFileStream.Create(RandomFileToCopy, fmCreate or fmOpenWrite or fmShareDenyWrite);
        try
          while FileStream1.Size < Fsize do
          begin
            Rand1 := Random(2147483600);
            FileStream1.WriteBuffer(Rand1, SizeOf(Rand1));
            //Label1.Text := IntToStr(FileStream1.Size);
            if breakit = true then break;
          end;
        finally
          FileStream1.Free;
        end;
      end;
      //memo1.lines.add('free space ' + IntToStr(CheckDiskSize(TPath.GetTempPath)));
      //memo1.BeginUpdate;
      //memo1.Lines.Add('End of CreateNewRandFile');
     // memo1.EndUpdate;
    
      //createnewrandfileActive := false;
    end;
    
    procedure TForm1.Button2Click(Sender: TObject);
    begin
      CreateNewRandFile(128000000); // create a 128mb file
    end;
    
    procedure TForm1.CopyData();
    var
      DriveStr : String;
      RandomFileToCopy : String;
      FileNameCounter : integer;
      FolderCounter : integer;
      FolderName : string;
      FolderArea : string;
      RandomFileName : String;
    begin
      copydataActive := true;
    
      breakit := false;
    
      FileNameCounter := 0;
      FolderCounter := 0;
    
      //memo1.BeginUpdate;
      //memo1.Lines.Add('Begin of Copy');
      //memo1.EndUpdate;
    
      DriveStr := TPath.GetTempPath() + TPath.DirectorySeparatorChar;
    
      //memo1.Lines.Add('DriveStr ' + DriveStr);
    
      RandomFileToCopy := DriveStr + 'random.fil';
    
      //memo1.Lines.Add('RandomFileToCopy ' + RandomFileToCopy);
    
      //memo1.Lines.Add('Creating Random File...');
    
      // create random file
      CreateNewRandFile(64000000); //64 mb file
    
      inc(FolderCounter);
      FolderName := 'rand' + IntToStr(FolderCounter);
      //memo1.Lines.Add('FolderName ' + FolderName);
    
      FolderArea := TPath.GetTempPath() + TPath.DirectorySeparatorChar + FolderName;
      //memo1.Lines.Add('FolderArea ' + FolderArea);
    
      while DirectoryExists(FolderArea) = true do
      begin
        inc(FolderCounter);
        FolderName := 'rand' + IntToStr(FolderCounter);
        FolderArea := TPath.GetTempPath() + TPath.DirectorySeparatorChar + FolderName;
    
        if breakit = true then break;
      end;
    
      TDirectory.CreateDirectory(FolderArea);
    
      while CheckDiskSize(FolderArea) > 0 do
      begin
        if breakit = true then break;
    
        //memo1.Lines.Add('CreateDir(FolderName) ' + FolderName);
    
        //FolderArea := FolderArea + TPath.DirectorySeparatorChar;
        //memo1.Lines.Add('FolderArea ' + FolderArea);
    
        RandomFileName := FolderArea + 'ran' + IntToStr(FileNameCounter) + '.fil';
        //memo1.Lines.Add('RandomFileName ' + RandomFileName);
    
        FileNameCounter := 0;
    
        while FileNameCounter<126 do
        begin
          inc(FileNameCounter);
    
          //while FileExists(RandomFileName) do
          //begin
          RandomFileName := FolderArea + TPath.DirectorySeparatorChar + 'ran' + IntToStr(FileNameCounter) + '.fil';
          //end;
    
          //memo1.Lines.Add(RandomFileName);
          //Label1.Text := RandomFileName;
    
          try
            //if FileExists(RandomFileName) = true then DeleteFile(RandomFileName);
            TFile.Copy(RandomFileToCopy, RandomFileName);
          Except
            On E: Exception Do
            begin
              //memo1.BeginUpdate;
              showmessage(E.ClassName + ' ERROR: ' + E.Message);
              //memo1.EndUpdate;
              breakit := true;
            end;
          end;
    
          if breakit = true then break;
    
        end;
    
        while DirectoryExists(FolderArea) = true do
        begin
          inc(FolderCounter);
          FolderName := 'rand' + IntToStr(FolderCounter);
          FolderArea := TPath.GetTempPath() + TPath.DirectorySeparatorChar + FolderName;
    
          if breakit = true then break;
        end;
    
        TDirectory.CreateDirectory(FolderArea);
    
      end;
    
      //memo1.BeginUpdate;
      //memo1.Lines.Add('End of Copy');
      //memo1.EndUpdate;
    
      //memo1.Lines.Add('Deleting Created Fillers');
    
      copydataActive := false;
    
    end;
    
    procedure TForm1.IdThreadComponent1Run(Sender: TIdThreadComponent);
    begin
      if breakit = true then
        begin
          IdThreadComponent1.Stop;
          exit;
        end;
    
      if copydataActive = false then
      begin
        AniIndicator1.Enabled := true;
        CopyData();
      end;
      sleep(500);
    end;
    
    procedure TForm1.IdThreadComponent1Stopped(Sender: TIdThreadComponent);
    begin
      AniIndicator1.Enabled := false;
    end;
    
    procedure TForm1.IdThreadComponent1Terminate(Sender: TIdThreadComponent);
    begin
      AniIndicator1.Enabled := false;
    end;
    
    procedure TForm1.STOPClick(Sender: TObject);
    begin
      breakit := true;
      IdThreadComponent1.Stop;
    end;
    
    end.