'Xamarin.Forms Android Write in External Storage SOLVED

I am a very beginner with C#, and I definitely need your help.
I am trying to create/Open/Write a .txt file in the External Storage, but I can not set the path with "Android.Content.Context.GetExternalFilesDir".

Here is my exact code:

string sourceFile = Path.Combine(Android.Content.Context.GetExternalFilesDir(null).AbsolutePath, "myFile.txt");
File.Create(sourceFile);

The error message is:" Android doesn't contain a definition for Content"

What should I do ? I don't know how to modify the assembly

EDIT:

I finally manage to do it. Thank you for your help, here it is how I did: 1. in my main project I created an interface (right click -> add -> interface) called Isave.cs. in this interface, I have write this code:

public interface Isave
    {
        void SaveFile(string FileName);
    }

2. In the Android project, I created a new class (right click -> add -> new class) called Save.cs in this class, I have write:

[assembly: Xamarin.Forms.Dependency(typeof(Save))]

namespace NAMEOFYOURAPP.Droid
{
    public class Save : Isave   
    {
        
        public void SaveFile(string fileName)
        {
            string path = Application.Context.GetExternalFilesDir(null).ToString();
            string filePath = System.IO.Path.Combine(path, fileName);
            File.WriteAllText(filePath, "test writting");

        }
        
    }
}   

3. Then in my main code MainPage.cs (where I want to use the function created), I used this code:

 DependencyService.Get<Isave>().SaveFile("myfile.txt");

In my case I used a button "SAVE" with only this line in the function for save my data.

Also in the Android.Manifest I add the 2 lines for the permission.

I also found out this video on youtube who show the process: here

Thank you again for your help, I hope my problem can help someone else.



Solution 1:[1]

I am still on the same point, here is what I did: in the Android Project, I created a class "Storage" (Storage.cs) with this function:

 public void SaveFile(string fileName)
    {
        string path = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
        string filePath = System.IO.Path.Combine(path, fileName);
        if (!System.IO.File.Exists(filePath))
        {
            using (System.IO.StreamWriter writer = new System.IO.StreamWriter(filePath, true))
            {
                writer.WriteLine(DateTime.Now.ToString() + ": " + "Example .txt created using Xamarin.");
            }
        }
        else
        {
            System.IO.File.Delete(filePath);
            using (System.IO.StreamWriter writer = new System.IO.StreamWriter(filePath, true))
            {
                writer.WriteLine(DateTime.Now.ToString() + ": " + "Example .txt created using Xamarin.");
            }
        }
    }

Then in the Forms Project, in MainPage.xaml.cs I added an interface with this code:

public interface IAccessFile
    {
        void SaveFile(string fileName);
    }

And then in the same item (MainPage.xaml.cs) in the main code, I added:

DependencyService.Get<IAccessFile>().SaveFile("workingHours.txt");

Also in AndroidManifest.xml, I added the permission:

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

The only code I did not add in the Android Project in my class Storage (before the function) is:

[assembly: Xamarin.Forms.Dependency(typeof(AccessFileImplement))]
namespace XamarinFirebase.Droid
{
    public class AccessFileImplement : IAccessFile

Because it doesn't work even with the using

Solution 2:[2]

Of course, if you create a Xamarin.Android project, you can use the android api easily in it.

In addition, you can also call the platform method with the dependency service. You can check the following link which shows how to use it to operate the android folder.

Link:Xamarin forms: How to create folder and a file in device external storage?

And then you need to add the write and read the external storage permission in the AndroidManifest.xml:

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

And grant the app the two permission with the permission request. You can check the following case.

Case:Can't write to Android External storage even with permissions set

In addition, you need to put the SaveFile(string fileName) method into the AccessFileImplement class.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Julien
Solution 2