0

My app writes files into a folder on the internal (or external) SD card but those files are not seen via MTP.

I found a solution in Java to utilize MediaScannerConnection.scanfile().Androidapi.JNI.Media which contains TJMediaScannerConnection but I did not find any help on how to use it.

An example would be appreciated.

Confuse
  • 5,646
  • 7
  • 36
  • 58
MichaSchumann
  • 1,361
  • 1
  • 17
  • 36

1 Answers1

2

You can use the following code:

use  Androidapi.Helpers, Androidapi.Jni.Media;

...

  procedure TForm1.UpdateMTP;
  var
    c: Integer;
    JMediaScannerCon: Androidapi.Jni.Media.JMediaScannerConnection;
    JMediaScannerCon_Client: Androidapi.Jni.Media.JMediaScannerConnection_MediaScannerConnectionClient;
  begin
    JMediaScannerCon:=TJMediaScannerConnection.JavaClass.init(SharedActivityContext, 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(sYourPath), nil);
      JMediaScannerCon.disconnect;
    end;
  end);

I use a polling to check if connected, a better way is to use the events of JMediaScannerCon, but I did not try it.

Enny
  • 759
  • 5
  • 8
  • This works perfectly, only windows seems to cache the content. If I disconnect and reconnect the device the fresh content is immediately visible. Deleted the comment about this problem. Enny - Thanks a lot, this saved my day. – MichaSchumann Oct 13 '14 at 18:59
  • I am very happy that it worked for you. So you can mark my answer as solution. – Enny Oct 15 '14 at 07:36