How to index a created file in Android sdcard Delphi

喜你入骨 提交于 2019-12-13 03:15:49

问题


In my code I create a .TXT file and store it in the shared folder "Download" like this:

procedure TF_start.Button2Click(Sender: TObject);
var
  path_file output_text: string;
begin
  path_file := TPath.Combine(System.IOUtils.TPath.GetSharedDownloadsPath, 'Folder_app');
  output_text := 'test';
  if not TDirectory.Exists(path_file) then
    TDirectory.CreateDirectory(path_file);

  try
    TFile.WriteAllText(TPath.Combine(path_file, Nome_Arquivo), Arquivo_saida);
  except
    ShowMessage('An error occurred while saving the file.');
  end;
end;

The file is perfectly created, but Android itself has a problem indexing the files so they can be read through Windows Explorer, so you have to rescan the folder that the file was created for so that it can be visible. There are even some apps on PlayStore that rescan the entire sdcard, but asking my client to install a secondary file to use my app is not a good choice ...

I found a code that theoretically performs this rescan on a specific folder, but it doesn't work. There are no errors, but the folder and the file continue is not visible in Windows Explorer ... The code is this:

procedure TF_corrida.BTNfinalize_appClick(Sender: TObject);
var
  c: Integer;
  JMediaScannerCon: Androidapi.Jni.Media.JMediaScannerConnection;
  JMediaScannerCon_Client: Androidapi.Jni.Media.JMediaScannerConnection_MediaScannerConnectionClient;
begin
    JMediaScannerCon:=TJMediaScannerConnection.JavaClass.init(TAndroidHelper.Context, JMediaScannerCon_Client);
    JMediaScannerCon.connect;
    c:=0;
    while not JMediaScannerCon.isConnected do begin
      Sleep(100);
      inc(c);
      if (c>20) then break;
    end;
    if (JMediaScannerCon.isConnected) then begin
      JMediaScannerCon.scanFile(StringToJString(path_file), nil);
      JMediaScannerCon.disconnect;
    end;
end;

Does anyone know why this code is not working? I even came to see that it didn't work in Delphi Tokyo, but I'm using Delphi Rio.

And yes, I correctly stated the READ and WRITE storage permissions in my code. The file is created correctly, just not visible.


回答1:


The cause for newly created files not being imediately visible in Windows Explorer does not lie in your code but in the way how MTP protocol works.

You see when you conect your android device to your computer using MTP protocol the device itself does provide your computer with list of files on it when your computer demands it but it does not support live update or noficiation of file changes.

The only protocol that would technically allow your computer to be notified of file changes on your andoid device is USB mass sotrage. But this protocol has limitation which requires that your computer gets exclusive access to file storage which would then prevent any program on your android device to make any cahnges to theese files while USB mass storage conection is active.

You can read a bit more about different connection protocols that android uses here:

Android USB Connections Explained: MTP, PTP, and USB Mass Storage



来源:https://stackoverflow.com/questions/57280831/how-to-index-a-created-file-in-android-sdcard-delphi

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