Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Saving files to external storage

Hello, everyone! I'm saving a backup from my app's database in SpecialFolder.Personal, it works, but I can't access the file nor move it to another directory, so I wanted to save it directly to the external storage. I already had a DependecyService to get the database location in Android, so I tried to use it to also get the External Storage directory, but when I click to save the backup file and browse in my phone/emulator I can't find it. Here's how I have done:

The class to get the path in the Android Project:

namespace VendasEstoque.Droid.Banco
        public string GetPath()
            string caminhoPasta = Android.OS.Environment.ExternalStorageDirectory.Path;

            return caminhoPasta;

In the main project I added the following to my DatabaseAcess Class:
string pt = dep.GetPath();

Also, I have a public interface (ICaminho) with the following:

     public interface ICaminho
            string ObterCaminho(string NomeArquivoBanco);
            string GetPath();

Finally, to create the backup file:

            private void BtnBackup_Clicked(object sender, EventArgs e)
                var dep = DependencyService.Get<ICaminho>();
                string db_path = dep.ObterCaminho("database.sqlite");
                string backup_path = Path.Combine(dep.GetPath(), "database.sqlite");

                var origin = File.ReadAllBytes(db_path);
                File.WriteAllBytes(backup_path, origin);

                DisplayAlert("BACKUP", "Backup Created!", "OK");

I appreciate any help. Thanks in advance!

Best Answer


  • LandLuLandLu Member, Xamarin Team Xamurai

    ExternalStorageDirectory returns the root path of your external storage and will be deprecated in API 29:
    If you want to store your files into your own application's external storage you can try:

    return Android.App.Application.Context.GetExternalFilesDir("").AbsolutePath;

    This returns path like: /storage/emulated/0/Android/data/com.companyname.packageName/files. It includes the package name and you will find your file in its own files folder.
    Here is how I store a simple plain text into the application's external storage:

    var folderPath = DependencyService.Get<ICaminho>().GetPath();
    var filePath = Path.Combine(folderPath, "sample.txt");
    System.IO.File.WriteAllText(filePath, "text");
  • Thanks, LandLu, but it doesn't recognize the AbsolutePath and without that it says it can't convert Java.IO.File[] to string.

  • LandLuLandLu Member, Xamarin Team Xamurai
    Accepted Answer

    Here is the sample. You can check the specific code.

  • I don't know what I was doing wrong, but now it worked fine. Thanks, @LandLu! :)

  • bronckhorstbronckhorst Member ✭✭

    When i try to obtain the external storage path with Android.OS.Environment.ExternalStorageDirectory.Path,
    i get the message "Android does not contain a definition for OS".

    What am i doing wrong?


  • Massimo_98Massimo_98 Member ✭✭

    @bronckhorst Might be a bit late but i believe you are getting the message because you are attempting to use the line of code in your shared project file. You should put it in the android file of your code. So create a cs file in the android section of your project and use it there. Android.OS is only accessible from the android section as far as i know, i could be wrong.

Sign In or Register to comment.