Xamarin 形式的文件下载 [英] File Download in Xamarin forms
问题描述
在 Xamarin.Forms 项目中,我试图从 blob 存储下载文件,我还需要将其保存在设备本地文件夹中并需要立即打开它.
In Xamarin.Forms project, I'm trying to download the file from the blob storage, also I need to save it in a device local folder and need to open it immediately.
我在打开文件时遇到此异常Fil:///LocalFolderUrlOfTheFile 通过 Intent.getData() 暴露在应用程序之外
I'm getting this exception while opening the file Fil:///LocalFolderUrlOfTheFile exposed beyond app through Intent.getData()
我仅在大于 Marshmallow 的 android 版本中遇到此异常.
I'm getting this exception only in android versions which are greater than Marshmallow.
这是我用来打开文件的代码:
Here are codes which i'm using to opening the file:
public void SaveandOpenFile(byte[] data, string fileName)
{
string externalStorageState = global::Android.OS.Environment.ExternalStorageState;
var externalPath = global::Android.OS.Environment.ExternalStorageDirectory.Path + "/" + global::Android.OS.Environment.DirectoryDownloads + "/" + fileName;
File.WriteAllBytes(externalPath, data);
Java.IO.File file = new Java.IO.File(externalPath);
file.SetReadable(true);
string application = "";
string extension = Path.GetExtension(externalPath);
// get mimeTye
switch (extension.ToLower())
{
case ".txt":
application = "text/plain";
break;
case ".doc":
case ".docx":
application = "application/msword";
break;
case ".pdf":
application = "application/pdf";
break;
case ".xls":
case ".xlsx":
application = "application/vnd.ms-excel";
break;
case ".jpg":
case ".jpeg":
case ".png":
application = "image/jpeg";
break;
default:
application = "*/*";
break;
}
//Android.Net.Uri uri = Android.Net.Uri.Parse("file://" + filePath);
Android.Net.Uri uri = Android.Net.Uri.FromFile(file);
Intent intent = new Intent(Intent.ActionView);
intent.SetDataAndType(uri, application);
intent.SetFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask);
Forms.Context.StartActivity(intent);
}
如果我遗漏了什么,请告诉我.提前致谢!
Please let me know if i'm missing something. Thanks in advance!
推荐答案
在新版本中,您不能在应用程序之外公开文件.你应该设置几件事情来做到这一点.请按照以下步骤操作:
You can't expose the File outside of your app in new versions. You should setup couple of things to do that. Follow these steps:
第 1 步: 转到您的 android 清单文件并在 application 标签内定义文件提供程序.
Step 1: Goto your android's manifest file and define file providers inside application tags.
<provider android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider" android:exported="false"
android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths" />
</provider>
第 2 步:在 XML 中定义提供程序路径
Step 2: Define the Provider path in XML
- 如果您的 Resources 文件夹中不存在 XML 文件夹,则创建该文件夹.
- 创建一个 XML 文件(如果您完全按照上一步操作,则该文件的名称将为provider_paths.xml"
第 3 步:在您在第 2 步中创建的提供者路径 XML 文件中定义提供者文件路径
Step 3: Define the Provider file path in Provider Path XML file you created in Step 2
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="files" path="."/>
<internal-path name="files" path="."/>
<files-path name="files" path="."/>
</paths>
第 4 步:当您下载文件时,您应该将它们保存在您在第 3 步中定义的位置.我将文件保存在 android 中的以下根目录:
Step 4: When you download your file you should save them in location that you defined in Step 3. I save the file in android at root directory below:
root = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
第五步: 打开文件,下面是我用来分享的方法.希望您理解并相应地进行更改:
Step 5: Opening the File, Following is the method that I'm using to share. Hope you understand and make changes on yours accordingly :
public Task Share(string title, string message, string filePath)
{
var extension = filePath.Substring(filePath.LastIndexOf(".",StringComparison.InvariantCultureIgnoreCase) + 1).ToLower();
var contentType = string.Empty;
Java.IO.File file=new Java.IO.File(filePath);
var apkURI = FileProvider.GetUriForFile(context, context.ApplicationContext.PackageName+ ".provider", file);
switch (extension)
{
case "pdf":
contentType = "application/pdf";
break;
case "png":
contentType = "image/png";
break;
default:
contentType = "application/octetstream";
break;
}
var intent = new Intent(Intent.ActionSend);
intent.SetFlags(ActivityFlags.GrantReadUriPermission);
intent.SetType(contentType);
intent.PutExtra(Intent.ExtraStream, apkURI);
intent.PutExtra(Intent.ExtraText, string.Empty);
intent.PutExtra(Intent.ExtraSubject, message ?? string.Empty);
var chooserIntent = Intent.CreateChooser(intent, title ?? string.Empty);
chooserIntent.SetFlags(ActivityFlags.ClearTop);
chooserIntent.SetFlags(ActivityFlags.NewTask);
context.StartActivity(chooserIntent);
return Task.FromResult(true);
}
如果您需要任何帮助,请告诉我.
Let me know if you need any help.
这篇关于Xamarin 形式的文件下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!