1

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

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Julien
  • 11
  • 2
  • 1
    is this in the Forms project, or the Android project? – Jason May 02 '22 at 18:02
  • It is in the Forms project (MainPage.xaml.cs) – Julien May 02 '22 at 19:13
  • 1
    that project is cross-platform so can't access platform-specific APIs. Forms Essentials provides a File System helper class to work around this, or see https://stackoverflow.com/questions/59628752/is-it-possible-to-access-external-storage-in-platform-independent-code-in-xamari – Jason May 02 '22 at 19:16
  • Thank you, I understand better what is my mistake now. I don't how to deal with it. If I create a new project only for android this will be easier? EDIT: How can I call a function written in my Android project to my Forms project? Maybe that can solve my problem ? – Julien May 02 '22 at 19:22

2 Answers2

0

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.

Liyun Zhang - MSFT
  • 8,271
  • 1
  • 2
  • 14
  • Thank you! for now I have try the solution found on this link: https://stackoverflow.com/questions/66469608/xamarin-forms-how-to-create-folder-and-a-file-in-device-external-storage the "update" part in the link, but when I try to start it, I have an error of access: Severity Code Description Project File Line Suppression State Error System.IO.IOException: Access to the path 'C:\Users\capta\Google Drive\Programmation\Programs APP\WorkingHours App\WorkingHours App\WorkingHours App.Android\obj\Debug\120\android\assets' is denied. – Julien May 03 '22 at 20:39
  • Do you grant the app the write and read the external storage permission?@Julien – Liyun Zhang - MSFT May 04 '22 at 01:02
  • You can check the edited answer.@Julien – Liyun Zhang - MSFT May 04 '22 at 01:12
  • Sorry for the answer, I have tried to answer to your message but I have answered to my own first message... – Julien May 05 '22 at 06:51
  • Has the problem been solved now?@Julien – Liyun Zhang - MSFT May 09 '22 at 00:36
  • I am trying to figure out how to do the "run-time permission check", as explained on this page: https://learn.microsoft.com/en-us/xamarin/android/app-fundamentals/permissions?tabs=windows But I'm a bit lost... I also discover the " .NET Standard libraries", but I don't know if it's necessary to work arround that or not – Julien May 09 '22 at 21:02
  • You can check the link which shows how to request the permission in my edited answer.@Julien – Liyun Zhang - MSFT May 10 '22 at 00:29
0

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

Julien
  • 11
  • 2
  • The problem is not solved, I am still with the "Access to path is denied". I have create a new project, with exactly the same code (without this part trying to read and write in the file). the project works well, but once I write back the code written above, I have the path denied, and If I try to delete only this part of the code to have back my app working, it do not work anymore, with the same error message – Julien May 06 '22 at 09:37
  • The permission write and read the external storage need to be granted by the user, so you can try to do a run-time permission check. – Liyun Zhang - MSFT May 06 '22 at 09:43