Request: ODK Collect to call MediaScannerConnection.ScanFile()


(Sebastian Therman) #1

What is the general goal of the feature?
Simplifying the transfer of finalized Collect data over MTP to another system.

What are some example use cases for this feature?
Because the Android media library isn't updated when files are written to the ODK folders, MTP transfers of Collect .xml instances (e.g. to Windows) require a reboot of the Android device or activating a separate app for rescanning media.
If Collect would call for updating the library for each finalized instance, such user fiddling won't be necessary, and the whole media library wouldn't need to be rescanned.

What can you contribute to making this feature a reality?

Collect only needs to call one extra command, as described here: https://issuetracker.google.com/issues/37071807#comment30

"Newly-created folders and newly-created files do not automatically appear in Windows File Explorer. New folders and new files do appear with the following workaround helpfully suggested by so many people (tested against API23):

MediaScannerConnection.scanFile( 
    this, 
    new String[]{ file.getAbsolutePath() }, // "file" was created with "new File(...)" 
    null, 
    null); 

Appending text to an existing file does NOT get updated by this workaround, for example, an application that continuously updates a log file."


(Sebastian Therman) #2

The official Android documentation is here:
https://developer.android.com/reference/android/media/MediaScannerConnection#scanFile(java.lang.String,%20java.lang.String)
The third argument, mimeTypes, would presumably be "text/xml".


(Yaw Anokwa) #3

@Sebastian_Therman MTP on Android is very buggy. What version of Android has this problem you are seeing? Is it easy to reproduce? Also, any reason why you can't use adb pull /sdcard/odk to pull the files?


(Sebastian Therman) #4

Thank you, Yaw, for the quick response!
Android versions: This problem has been present in Android from the very beginning. As a known and tracked bug, it is not fixed in any version that I know of, but I have verified up to version Oreo (8.1).

Reproducing the bug: create or delete any file on the Android device in an MTP-visible folder and refresh the directory view on the external device (e.g. in Windows Explorer), but the changes will not be visible. Rebooting Android or running a Media rescan with an app such as media.Re.Scan will correct the media library listing.

Reasons not to use ADB for file transfer: requires turning on USB debugging (exposing devices), requires driver/software installations at the Windows end.

Since this fix could be a single line of code (?), I thought maybe it would be trivial to implement. Of course, if caution necessitates making it an optional feature and adding an option selector to Collect, then it'd require more work.


(Yaw Anokwa) #5

@Sebastian_Therman You seem pretty technical! Would you be open to making this change in Collect to see if it works as you expect?


(Sebastian Therman) #6

Thank you, Yaw, for your confidence. :grinning:
I have never contributed code to any open source project, and haven't familiarised myself with the Collect code, so the overhead for my trying the change would be substantial. If there's a veteran who is willing, that'd be vastly preferable.
This is all Google's fault, of course, for implementing an MTP database which isn't in sync with the filesystem... :pensive:


(Yaw Anokwa) #7

Veterans have to start somewhere, no? This issue isn't on our critical path so you can take as much time as we'd like. The reality is there isn't an abundance of developers in the wings waiting to fix things, so if this is important to you, the best way to get it done is to take a stab at it. On our part, we'll do our best to help as you take your first steps to being a code contributor to Collect.