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.

1 Answer 1

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.

2
  • 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. Oct 13, 2014 at 18:59
  • I am very happy that it worked for you. So you can mark my answer as solution.
    – Enny
    Oct 15, 2014 at 7:36

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.