如何在Android SD卡Delphi中索引已创建的文件 [英] How to index a created file in Android sdcard Delphi

查看:176
本文介绍了如何在Android SD卡Delphi中索引已创建的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中,我创建一个.TXT文件,并将其存储在共享文件夹下载"中,如下所示:

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;

文件创建完美,但是Android本身在为文件建立索引时存在问题,因此可以通过Windows资源管理器读取它们,因此您必须重新扫描创建该文件的文件夹,以便使其可见. PlayStore上甚至有一些应用程序可以重新扫描整个sdcard,但要求我的客户端安装辅助文件以使用我的应用程序不是一个好选择...

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 ...

我找到了一个理论上可以在特定文件夹上执行此重新扫描的代码,但是它不起作用.没有错误,但是在Windows资源管理器中看不到文件夹和文件继续" ... 代码是这样的:

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;

有人知道为什么此代码不起作用吗?我什至看到它在Delphi Tokyo中不起作用,但我正在使用Delphi Rio.

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.

是的,我在我的代码中正确声明了READ和WRITE存储权限.该文件创建正确,只是不可见.

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

推荐答案

在Windows资源管理器中无法立即看到新创建的文件的原因不在于您的代码,而是在于MTP协议的工作方式.

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.

您会看到,当您使用MTP协议将android设备连接到计算机时,设备本身会在计算机需要时向计算机提供文件列表,但它不支持实时更新或文件更改通知.

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.

从技术上来讲,唯一可让您的计算机收到有关Andoid设备上文件更改的通知的协议是USB大容量存储.但是此协议有局限性,要求您的计算机具有对文件存储的独占访问权限,这将阻止您的android设备上的任何程序在USB大容量存储连接处于活动状态时对这些文件进行任何更改.

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.

您可以在此处详细了解android使用的不同连接协议:

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

Android USB解释的连接:MTP,PTP和USB海量存储

这篇关于如何在Android SD卡Delphi中索引已创建的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆