0

I would like that the users of my Android application be able to copy data files to their computer via USB. They should additionaly be able to erase the files on the android device once copied. To this end, my application creates .csv files which are stored in the external storage space, as recommended by Google. I have created the destination folder thanks to this method:

static File getMyStorageDir() {
           File file = newFile(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_DOCUMENTS), "MyDir");
    if (!file.mkdirs()) {
        Log.e( "LogFile", "Directory not created");
    }
    return file;
}

On the Android device I can browse the .csv files in "/sdcard/Documents/MyDir". When I connect the device to a Win 10 computer in MTP mode, the "Documents" folder does not appear. If I try to create the "Documents" folder in the Windows explorer, it will fail, (probably) proving that the folder exists, but is hidden. On the other hand I can create a folder "foo" in Windows and put files into it. The "foo" folder and its content will appear in the browser (ES File Explorer) on the device after a while. To finish, I can erase files in the "DCIM/Camera" folder...

I think I am close but I am missing something...

  • 1
    the device needs to update it's mtp database before it will show the file to windows. Automated scanning happens on device boot but usually not afterwards. You can force it to do so via http://stackoverflow.com/questions/9414955/trigger-mediascanner-on-specific-path-folder-how-to – zapl Jun 02 '16 at 09:21
  • Thanks for the link. I have tried to "trick the OS" with a sendBroadcast(), but it does not work currently - I get an exception. I will dig into that later. – Michel Kuenemann Jun 02 '16 at 12:42
  • the mediascannerconnection thing should work fine on all versions of android (for files) - http://stackoverflow.com/questions/31157882/mediascannerconnectionscanfile-converts-directories-into-files-when-accessing-t – zapl Jun 02 '16 at 12:49
  • OK - I think I will investigate this seriously because I have noticed that the synchro between the Android and Windows is very laggy... – Michel Kuenemann Jun 02 '16 at 13:08
  • 1
    I have added the following magical line in my code, after the new file creation: MediaScannerConnection.scanFile ( context, new String[] { mStoreDir }, null, null); I can see the content of the windows browser getting updated in real time. Perfect. – Michel Kuenemann Jun 02 '16 at 15:15

1 Answers1

2

The MTP cache gets out of date until a reboot of the phone.

A workaround is:

Clear the "Media Storage" app's data and restart the device

Abdullah
  • 935
  • 9
  • 18