0
votes

I am using SQLite (via the "sqlite-net-pcl" nuget package) within a Xamarin Forms application running on various versions of Android.

I need to make the database file available for download to a PC (even if only when in debug mode) for diagnostics purposes, but it seems whichever Android folder I place it in I can never find it afterward.

I am (by default) placing the file in...

System.IO.Path.Combine(Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "database.db")

Note that in the above I am using the Mono libraries not the Android libraries to find the folder.

I have tried most of the "Environment.SpecialFolder" options, what I would like ideally is a way to determine the path to the "Downloads" folder that is available when browsing to the device from the PC over USB (This PC\MyDevice\Internal shared storage\Download) but I just cannot find a way to determine where that folder is located across different devices and Android versions.

Can somebody please tell me how to get the path to that "Downloads" folder, or even how to get to the parent of that folder so that I can create my own subfolder? I suspect that I will need to be using the Android.OS.Environment.DirectoryDownloads or similar but I just do not seem to be able to get that right.

Thanks.

1
The reason you cannot find it because it is hidden, Its the Android firewall trying to hide this data from external apps and devices, Why do you need this anyway? - FreakyAli
As I said, I want to be able to download the database file from the device for diagnostics purposes. In normal operation, this would not be required. - Martin Robins

1 Answers

0
votes

You can do this natively on Android something like this :

File sd = Environment.GetExternalStoragePublicDirectory(Environment.ExternalStorageDirectory.Path);
if(sd.CanWrite())
{
       string currentDBPath = sqlConnectionObj.DatabasePath;
       string backupDBPath = "backupname.db";
       File currentDB = new File(currentDBPath);
       File backupDB = new File(sd, backupDBPath);
       if(currentDB.Exists())
        {
           FileChannel src = new FileInputStream(currentDB).Channel;
           FileChannel dst = new FileOutputStream(backupDB).Channel;
           dst.TransferFrom(src, 0, src.Size());
           src.Close();
           dst.Close();          
        }
}          

This should create a database file in your sd card root path

Oh yeah, Don't forget to set the permission to write on SD card in your manifest, like below.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Also you might have to change check runtime permissions if not available already.