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