How to list files from an android firemonkey folder?

Asked

Viewed 2,173 times

1

I have to create a way for the user to search for a backup generated by my system between the folders of the device, so I thought of loading the folders in a listview as it is clicked would enter the sub-folders.

My problem is how to load folders? I found some examples for Delphi, but I couldn’t get them to work on Firemonkey.

  • Out of curiosity, but why would the user need this backup?.. at what point.

  • if user changes device, it can recover all data

1 answer

3


I could do it this way

Listview and Label Components

Uses System.IOUtils

At the Form Show event

carregadiretorio(GetSharedDownloadsDir);

At Listview’s Itemclickex event

if ListView1.Items[ListView1.ItemIndex].Detail='folder' then
  carregadiretorio(LbFolder.Text+PathDelim+ListView1.Items[ListView1.ItemIndex].Text)
else if ListView1.Items[ListView1.ItemIndex].Detail='voltar' then
  carregadiretorio(copy(ExtractFilePath(LbFolder.text), 0,Length(ExtractFilePath(LbFolder.text))-1));

and the database that loads the directory

procedure carregadiretorio(diretorio: string);
var
  listapastas, listaarquivos: TStringDynArray;
  pasta, arquivo: string;
  LItem: TListViewItem;
begin

    ListView1.Items.Clear;
    LbFolder.Text := diretorio;
    listapastas := TDirectory.GetDirectories(diretorio);
    listaarquivos := TDirectory.GetFiles(diretorio);
    ListView1.BeginUpdate;

    LItem := ListView1.Items.Add;
    LItem.Detail := 'voltar';
    LItem.Text := '..<<';

    for pasta in listapastas do
    begin
      //Carrega as Pastas
      LItem := ListView1.Items.Add;
      LItem.Detail := 'folder';
      LItem.Text := Copy(pasta, Length(ExtractFilePath(pasta))+1, Length(pasta));
    end;

    for arquivo in listaarquivos do
    begin
      //Carrega os Arquivos
      LItem := ListView1.Items.Add;
      LItem.Detail := 'file';
      LItem.Text := ExtractFileName(arquivo);
    end;

    ListView1.EndUpdate;
end;

Another solution follows the link http://bluexmas.tistory.com/427

  • Very good, only you have to add a new class, Androidapi.JNI.Interfaces.pas It will help me a lot when I need it.

  • 1

    I suggest replacing / for PathDelim s:=(LbFolder.Text + PathDelim + ListView1.Items&#xA; [ListView1.ItemIndex].Text); as in windows gave Directory not found error

Browser other questions tagged

You are not signed in. Login or sign up in order to post.