[英]File Association in Xamarin Forms (Android not working)
該應用程序需要從 Web 下載以及郵件附件接收 XML 文件。 在 iOS 上一切正常,但在 Android 上,文件關聯不起作用。 程序運行良好,但在嘗試打開 XML 文件時,Android 顯示“無法打開文件”
我嘗試了不同的 MemeTypes、DataShemes 和 Actions,但都沒有奏效。
[Activity(Label = "MyApp", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, LaunchMode = Android.Content.PM.LaunchMode.SingleTask,
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
[IntentFilter(new[] { Intent.ActionView},
Categories = new[] { Intent.CategoryDefault },
DataScheme = "file",
DataHost = "*",
DataPathPattern = ".*\\.xml",
DataMimeType = @"application/xaml+xml")]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
string action = Intent.Action;
string type = Intent.Type;
//If opened to recieve a file
if (Intent.ActionView.Equals(action) && !String.IsNullOrEmpty(type))
{
Console.WriteLine("Opened to recieve a file!");
}
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
OxyPlot.Xamarin.Forms.Platform.Android.PlotViewRenderer.Init();
LoadApplication(new App());
}
我希望該應用程序用於打開和發送 .xml 文件,然后將對其進行處理。
編輯:鏈接到測試應用程序,其中控制台在工作時喊出“已打開以接收文件”。 https://dracoon.team/#/public/shares-downloads/BMCPjb5XGlWyQArahpnl2x1uoHmBCRv6
對於正在尋找如何將您的應用程序與open with...
相關聯的解決方案的人open with...
(我無法找到這個問題的明確答案)。 這是對我有用的:
我正在使用 Xamarin 16.7
就像路易斯(OP)一樣,我正在嘗試以下IntentFilter
:
[IntentFilter(new[] { Intent.ActionView},
Categories = new[] { Intent.CategoryDefault },
DataScheme = "file",
DataHost = "*",
DataPathPattern = ".*\\.txt",
DataMimeType = @"*/*")]
為了測試上述意圖過濾器,我會轉到默認文件應用程序,並嘗試打開一個 txt 文件,但我的應用程序不會顯示為選項。
然后我把它改成了:
[IntentFilter(new[] { Intent.ActionView },
Categories = new[] { Intent.CategoryDefault }, DataMimeType = @"text/plain")]
它奏效了。 問題是在DataScheme
,當嘗試open with...
,它不是file
,它實際上是content
。
如果您遇到此類問題,請確保過濾器設置正確。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.