From b1aec9c411f2587518ccb6bf69f2dfb6c18370ba Mon Sep 17 00:00:00 2001 From: Jon Date: Fri, 4 Sep 2015 17:46:13 -0400 Subject: [PATCH] Add Android sharing --- ShareExample/Droid/MainActivity.cs | 41 +++++++++++++++---- .../Droid/Resources/Resource.designer.cs | 21 ++++++++++ ShareExample/iOS/AppDelegate.cs | 9 ++-- 3 files changed, 57 insertions(+), 14 deletions(-) diff --git a/ShareExample/Droid/MainActivity.cs b/ShareExample/Droid/MainActivity.cs index 0bcbe34..4e5d92a 100644 --- a/ShareExample/Droid/MainActivity.cs +++ b/ShareExample/Droid/MainActivity.cs @@ -1,26 +1,49 @@ -using System; - -using Android.App; -using Android.Content; +using Android.App; using Android.Content.PM; -using Android.Runtime; -using Android.Views; -using Android.Widget; using Android.OS; +using Xamarin.Forms; +using Android.Content; +using Android.Graphics; +using Xamarin.Forms.Platform.Android; namespace ShareExample.Droid { [Activity(Label = "ShareExample.Droid", Icon = "@drawable/icon", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity { + const int ShareImageId = 1000; + protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); - global::Xamarin.Forms.Forms.Init(this, bundle); + Forms.Init(this, bundle); LoadApplication(new App()); + + MessagingCenter.Subscribe (this, "Share", Share, null); + } + + async void Share (ImageSource imageSource) + { + var intent = new Intent (Intent.ActionSend); + intent.SetType ("image/png"); + + var handler = new ImageLoaderSourceHandler(); + var bitmap = await handler.LoadImageAsync(imageSource, this); + + var path = Environment.GetExternalStoragePublicDirectory (Environment.DirectoryDownloads + + Java.IO.File.Separator + "logo.png"); + + using (var os = new System.IO.FileStream (path.AbsolutePath, System.IO.FileMode.Create)) { + bitmap.Compress (Bitmap.CompressFormat.Png, 100, os); + } + + intent.PutExtra (Intent.ExtraStream, Android.Net.Uri.FromFile (path)); + + var intentChooser = Intent.CreateChooser (intent, "Share via"); + + StartActivityForResult (intentChooser, ShareImageId); } } } - diff --git a/ShareExample/Droid/Resources/Resource.designer.cs b/ShareExample/Droid/Resources/Resource.designer.cs index b911c7f..a3e8b3b 100644 --- a/ShareExample/Droid/Resources/Resource.designer.cs +++ b/ShareExample/Droid/Resources/Resource.designer.cs @@ -26,6 +26,8 @@ static Resource() public static void UpdateIdValues() { + global::Xamarin.Forms.Platform.Resource.String.ApplicationName = global::ShareExample.Droid.Resource.String.ApplicationName; + global::Xamarin.Forms.Platform.Resource.String.Hello = global::ShareExample.Droid.Resource.String.Hello; } public partial class Attribute @@ -56,6 +58,25 @@ private Drawable() { } } + + public partial class String + { + + // aapt resource value: 0x7f030001 + public const int ApplicationName = 2130903041; + + // aapt resource value: 0x7f030000 + public const int Hello = 2130903040; + + static String() + { + global::Android.Runtime.ResourceIdManager.UpdateIdValues(); + } + + private String() + { + } + } } } #pragma warning restore 1591 diff --git a/ShareExample/iOS/AppDelegate.cs b/ShareExample/iOS/AppDelegate.cs index 909397b..ed0df28 100644 --- a/ShareExample/iOS/AppDelegate.cs +++ b/ShareExample/iOS/AppDelegate.cs @@ -11,7 +11,7 @@ public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsAppli { public override bool FinishedLaunching(UIApplication app, NSDictionary options) { - global::Xamarin.Forms.Forms.Init(); + Forms.Init(); // Code for starting up the Xamarin Test Cloud Agent #if ENABLE_TEST_CLOUD @@ -28,9 +28,9 @@ public override bool FinishedLaunching(UIApplication app, NSDictionary options) async void Share (ImageSource imageSource) { var handler = new ImageLoaderSourceHandler(); - var bytes = await handler.LoadImageAsync(imageSource); + var uiImage = await handler.LoadImageAsync(imageSource); - var item = NSObject.FromObject (bytes); + var item = NSObject.FromObject (uiImage); var activityItems = new[] { item }; var activityController = new UIActivityViewController (activityItems, null); @@ -38,8 +38,7 @@ async void Share (ImageSource imageSource) while (topController.PresentedViewController != null) { topController = topController.PresentedViewController; } - topController.PresentViewController (activityController, true, () => { - }); + topController.PresentViewController (activityController, true, () => {}); } } }