我正在为Android和iOS开发Xamarin Form应用程序。我想从另一个应用程序(如Whatsapp或文本消息传递等)接收图像,当您选择图像并单击“共享”时,我的应用程序出现在共享列表选项中,然后选择我的应用程序,并在我的应用程序上显示预览图片。
[我当时在寻找不同的帖子,但没有找到一个很好的示例来说明如何在我的应用程序中接收图像,特别是iOS。
对于Android:
在IntentFilter
中定义MainActivity
:
[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);
}
}
For ios
您应该创建一个share extension
,然后将引用添加到您的ios项目中,请参考the similar case