How to index a created file in Android sdcard Delphi

  

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.

Comments are closed.