Xamarin 表单从其他应用程序接收图像
Xamarin Form Receive Image from other app
我正在为 Android 和 iOS 开发 Xamarin Form 应用程序。我想接收来自其他应用程序的图片,例如 Whatsapp 或短信等,当您 select 图片并点击分享时,我的应用程序会出现在分享列表选项中,然后 select 我的应用程序,并在我的应用程序上显示图像预览。
我查看了不同的帖子,但没有找到一个很好的例子来说明如何在我的应用程序上接收图像,特别是 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项目的引用,参考the similar case
我正在为 Android 和 iOS 开发 Xamarin Form 应用程序。我想接收来自其他应用程序的图片,例如 Whatsapp 或短信等,当您 select 图片并点击分享时,我的应用程序会出现在分享列表选项中,然后 select 我的应用程序,并在我的应用程序上显示图像预览。
我查看了不同的帖子,但没有找到一个很好的例子来说明如何在我的应用程序上接收图像,特别是 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项目的引用,参考the similar case