Android - The case of the vanishing file

I’m relatively new to the Android development platform, coming from the Windows desktop. One of the great things about recent editions of Delphi is it’s ability target numerous other platforms. One of the worst things about recent versions of Delphi is it’s ability to target platforms you are not as familiar with. If your use case is atypical, you can quickly find yourself swimming in shark infested waters. You never know what is going to byte you, a bug in the platform, a bug in Delphi RTL or FMX code, a bug in your code, or just your lack of knowledge about the platform.

Today I was trying to figure out why files I was saving to the Documents folder on my Android LG5 were visible from the Android File Manager, but not present when I connected the phone to a Windows, or Ubuntu desktop. Turns out there is a long outstanding bug in Android that google has not even acknowledged. In order to see new files when connecting to a device using the MTP protocol, you may need to re-boot the Android device before the files appear. Indeed once I did so, my files appeared and I could copy them off to my PC. You may want to comment on this motion to get the issue re-opened and addressed.

Hopefully this helps someone else avoid spending a lot of time scratching their head…

2 Responses to “Android - The case of the vanishing file”

  1. Glenn D Says:

    You don’t need to reboot the phone for the file to appear over MTP.

    You can broadcast an intent to the system that kicks the media scanner in the guts and will re-index the files and thus show them over MTP.

    This piece of code will force the media scanner to re-index the given file:

    procedure SendRefreshIntent(const AFileName: string);
    var
    LIntent: JIntent;
    begin
    LIntent := TJIntent.Create;
    LIntent.setAction(TJIntent.JavaClass.ACTION_MEDIA_SCANNER_SCAN_FILE);
    LIntent.setData(StrToJURI(’file://’ + AFileName));
    MainActivity.sendBroadcast(LIntent);
    end;

  2. Larry Hengen Says:

    @Glenn,

    Thanks for the info. I will look at incorporating this into my app so files appear immediately.

Leave a Reply