mauimaui-android

How to copy files from app folder to Downlaod in MAUI.net application for android 11?


I've a MAUI.net app for android that use a sqlite db (mydb.db); I need to export it as backup, and if needed to import it for restore. I can't find any working example that allow to copy a file from the app folder to android download or documents or sd card, for target from A11. Any advice? I'm not asking for opinion, I'm looking for real word working sample for MAUI, xamarin samples failed on A11 - A13


Solution

  • You can call the platform's native code to solve the problem.please refer to android.

    #if ANDROID
                //Destination path
                var destinationPath = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).AbsolutePath;
                //Source Path
                string sourceFilePath = Path.Combine(FileSystem.AppDataDirectory, "mydb.db");
                using (FileStream sourceStream = new FileStream(sourceFilePath, FileMode.OpenOrCreate))
                using (FileStream destinationStream = new FileStream(Path.Combine(destinationPath, "test.db"), FileMode.Create)) {                    
                    sourceStream.CopyTo(destinationStream);
                }
    #endif