问题
I'm mainly developing for Android, so I know one can access the external storage of an Android device in Xamarin.Forms using Android.Content.Context.GetExternalFilesDir
.
I know its possible to access the internal storage platform-independently using Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
, but is it also possible to do the same for external storage?
I know the use case might be prettty limited, as iOS doesn't allow any non-jailbreaked access to the file system anyway, but wouldn't it still be good to write to external file systems without having to implement it for each platform? Couldn't one just write to the root app files directory and let the OS API handle the rest? I'm pretty new so don't laugh too much at the stupid question.
Many thanks for any help.
回答1:
Forms does not provide the api to access External storage , we have to implement it in each specific platform ,please check https://docs.microsoft.com/en-us/xamarin/android/platform/files/external-storage?tabs=windows
回答2:
Android
My code for the Android project, I use a class name like "Storage", extend the Interface you create in the Xamarin.Forms project and use:
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.");
}
}
}
/// <summary>
/// To receive file from Android Phone
/// </summary>
public string ReceiveFile(string fileName)
{
string path = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
string filePath = System.IO.Path.Combine(path, fileName);
return filePath;
}
Xamarin.Forms Interface:
public interface IArchivos
{
void SaveFile(string fileName);
string ReceiveFile(string fileName);
}
Hope this helps!
回答3:
Xamarin For Android Implementation.
// First take storage permission.
// var status = await Permissions.RequestAsync<Permissions.StorageRead>();
// status = await Permissions.RequestAsync<Permissions.StorageWrite>();
var fullPath = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, "file_name.txt");
FileInfo fi = new FileInfo(fullPath);
// Writing data to file.
var fsToWrite = fi.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
var sw = new StreamWriter(fsToWrite);
sw.WriteLine("Another line from streamwriter");
sw.Close();
fsToWrite.Close();
// Reading data from file.
var fsToRead = fi.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
var sr = new StreamReader(fsToRead);
string fileContent = sr.ReadToEnd();
sr.Close();
fsToRead.Close();
System.Console.WriteLine("Data from : ", fileContent);
来源:https://stackoverflow.com/questions/59628752/is-it-possible-to-access-external-storage-in-platform-independent-code-in-xamari