提取zip文件时,Android应用程序无响应

时间:2019-01-07 08:37:51

标签: android delphi zip

我想通过FMX And​​roid应用程序提取大型zip文件,但是Android看到该应用程序没有响应,并建议将其杀死。

这是我的代码:

procedure AddSoundRes(SfileN: string);
begin
  if trim(SfileN) = '' then ShowMessage('Please Select A File')
  else
    begin
      try
        FormMessage.Show;
        Application.ProcessMessages;
        Archive2 := TZipFile.Create;
        Archive2.Open(SfileN, zmRead);
        Archive2.ExtractZipFile(SfileN, soundpath);
        ShowMessage('Resource added successfully');
      finally
        Archive2.Free;
        FormMessage.Hide;
      end;
    end;
end;

如何解决?

1 个答案:

答案 0 :(得分:2)

您需要在另一个线程中执行此操作。您不能执行如此长的任务,并且期望在同一线程中完成所有操作后,UI才能可用。

这是我为您制作的一个示例:

unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Controls.Presentation,
  FMX.StdCtrls, System.Zip;

type
  TForm1 = class(TForm)
    btn1: TButton;
    procedure btn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

type
  TExtractZip = class(TThread)
  private
    fZIP: string;
  protected
    procedure Execute; override;
  public
    constructor Create(const aZipFile: string);
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

{ TExtractZip }

constructor TExtractZip.Create(const aZipFile: string);
begin
  inherited Create(True);
  FreeOnTerminate := True;
  fZIP := aZipFile;
end;

procedure TExtractZip.Execute;
begin
  if TZipFile.IsValid(fZIP) then
    TZipFile.ExtractZipFile(fZIP, '.\contents\');
end;

procedure TForm1.btn1Click(Sender: TObject);
var
  extractzip: TExtractZip;
begin
  extractzip := TExtractZip.Create('.\azipfile.zip');
  extractzip.Start;
end;

end.