Xamarin Form从其他应用程序接收图像



我正在为Android和iOS开发Xamarin Form应用程序。我想从另一个应用程序(如Whatsapp或短信等(接收图像,当您选择图像并单击"共享"时,我的应用程序会出现在共享列表选项中,然后选择我的应用,并在我的应用上显示图像预览。

我在看不同的帖子,但我没有找到一个很好的例子来说明如何在我的应用程序上接收图像,这在iOS中很特别。

对于Android

MainActivity:中定义IntentFilter

[Activity(Label = "@string/app_name", Theme = "@style/MyTheme", MainLauncher = true,LaunchMode =LaunchMode.SingleTask)]
[IntentFilter(new string[] { Intent.ActionSend },Categories = new string[] { Intent.CategoryDefault },DataMimeType = "image/*")]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{   
//if your activity LaunchMode not SingleTask or SingleTop,when you receive the share image,it will trigger in the OnCreate method
Android.Net.Uri imageUri = (Android.Net.Uri)intent.GetParcelableExtra(Intent.ExtraStream);
.....
}
//if lanch mode is singletask or singletop,it will trigger in the OnNewIntent method(This is recommended)
protected override void OnNewIntent(Intent intent)
{
base.OnNewIntent(intent);
Android.Net.Uri imageUri = (Android.Net.Uri)intent.GetParcelableExtra(Intent.ExtraStream);
}

}

对于ios

您应该创建一个share extension,然后将引用添加到您的ios项目中,参考类似的案例

最新更新