在共享存储 Xamarin 表单中保存文件 [英] Saving a file in shared storage Xamarin forms
问题描述
我需要在android的共享存储中保存一个文件.我遇到了这个链接 =>
在您的 Android 项目中使用它来将流保存到文件:
public async Task SaveAndView(string fileName, String contentType, MemoryStream stream){尝试{字符串根=空;//获取android设备的根路径.如果(Android.OS.Environment.IsExternalStorageEmulated){root = Android.OS.Environment.ExternalStorageDirectory.ToString();}别的root = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);//创建目录和文件Java.IO.File myDir = new Java.IO.File(root + "/meusarquivos");myDir.Mkdir();Java.IO.File file = new Java.IO.File(myDir, fileName);//如果文件存在则删除if (file.Exists()) file.Delete();//将流写入文件FileOutputStream outs = new FileOutputStream(file);outs.Write(stream.ToArray());outs.Flush();关闭();}捕获(异常前){PostLog.AppCenterLogExcecao(ex, new Dictionary { { "origem", "OrderViewModel - 159" } });}}
在您的共享代码中:
await DependencyService.Get().SaveAndView(OrderId.ToString() + .pdf", application/pdf", stream);
请务必在使用代码前询问权限.
I need to save a file in the shared storage of android. I came across this link => https://developer.android.com/training/data-storage/shared/documents-files
I am using the dependency service and I am able to successfully save a file to desired location. But I am able to create only a blank file. I need to write some content into that file. Actually I created a thread few hours ago with android tag and got the solution where I had to override the OnActivityResult method and get the intent data. Now I have done it and I am able to get the intent data. But Now I dont know which path should i choose from the intent and how to open the file using the chosen path and how to write content into that chosen file. Any android + xamarin expert should be able to help me..
The android platform code to implement the write service:
Activity _activity;
private static int CREATE_FILE = 6835;
public WriteFileService()
{
_activity = CrossCurrentActivity.Current.Activity;
}
void IWriteService.WriteFile(string Content)
{
Intent intent = new Intent(Intent.ActionCreateDocument);
intent.AddCategory(Intent.CategoryOpenable);
intent.SetType("application/txt");
intent.PutExtra(Intent.ExtraTitle, "Invoice.txt");
_activity.StartActivityForResult(intent, CREATE_FILE);
}
The overriden OnActivityResultMethod:
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
Toast.MakeText(Application.Context, "requestCode is " + requestCode, ToastLength.Short).Show();
if (requestCode == 6835)
{
if (data != null)
{
Toast.MakeText(Application.Context,
data.GetType().ToString(),
ToastLength.Short).Show();
}
}
base.OnActivityResult(requestCode, resultCode, data);
}
This is the screen of the Intent Data from OnActivityResult
Use this in your Android project to save a stream to file:
public async Task SaveAndView(string fileName, String contentType, MemoryStream stream)
{
try
{
string root = null;
//Get the root path in android device.
if (Android.OS.Environment.IsExternalStorageEmulated)
{
root = Android.OS.Environment.ExternalStorageDirectory.ToString();
}
else
root = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
//Create directory and file
Java.IO.File myDir = new Java.IO.File(root + "/meusarquivos");
myDir.Mkdir();
Java.IO.File file = new Java.IO.File(myDir, fileName);
//Remove if the file exists
if (file.Exists()) file.Delete();
//Write the stream into the file
FileOutputStream outs = new FileOutputStream(file);
outs.Write(stream.ToArray());
outs.Flush();
outs.Close();
}
catch (Exception ex)
{
PostLog.AppCenterLogExcecao(ex, new Dictionary<string, string> { { "origem", "OrderViewModel - 159" } });
}
}
And in your shared code:
await DependencyService.Get<ISave>().SaveAndView(OrderId.ToString() + ".pdf", "application/pdf", stream);
Be sure to ask for the permissions before using the code.
这篇关于在共享存储 Xamarin 表单中保存文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!