Warm tip: This article is reproduced from serverfault.com, please click

External storage files not visible to laptop over USB

发布于 2020-11-28 12:46:15

In an Android app (API 29) I'm writing a file that needs to be readable from a laptop over USB. It is written to emulated external storage. As expected, in an Android file manager on the physical device, I see the file at:

Internal Storage > Android > data > com.xxx.myapp > files > Directory Documents >
Exports > Databases > my file.dbf

However, from a USB-connected laptop I do not see the file using Android File Transfer (or even Android Studio's Device File Explorer). I do see private files at:

Internal Storage > Android > data > data > com.xxx.myapp > files > ...

Storage permission is enabled in app settings and USB debugging is enabled in developer settings.

Although it's apparently not supposed to be necessary, I've enabled the manifest settings ReadExternalStorage and WriteExternalStorage as well.

What must I do to make the file visible to the laptop?

My code is below (within a Xamarin Android dependency service).

        public async Task WriteExternalFileFromStreamAsync ( string foldername, string subfoldername, string filename, Stream stream )
        // Writes external file from given stream
        {
            await Task.Run ( () =>
            {
                try
                {
                    // Create folder
                    Context  currentContext = Android.App.Application.Context;
                    Java.IO.File directory = new Java.IO.File ( currentContext.GetExternalFilesDir( "directorydocuments" ).AbsolutePath + "/" + foldername );
                    directory.Mkdir ();

                    // Create subfolder
                    Java.IO.File subdirectory = new Java.IO.File ( currentContext.GetExternalFilesDir( "directorydocuments" ).AbsolutePath + "/" + foldername + "/" + subfoldername );
                    subdirectory.Mkdir ();
                    
                    // Write file
                    string path = Path.Combine ( subdirectory.AbsolutePath, filename );
                    using ( FileStream filestream = new FileStream ( path, FileMode.Create, FileAccess.ReadWrite ) )
                        stream.CopyTo ( filestream );
                }
                catch ( Exception ex )
                {
                    MyUtil.WriteLogFile ( S.Exception, ex.AsStringVal () );
                    throw;
                }
            });
        }
Questioner
BillF
Viewed
0
BillF 2020-11-29 12:16:01

It's working now, after various restarts / reboot. Sorry.