Skip to content

Commit

Permalink
Add Android sharing
Browse files Browse the repository at this point in the history
  • Loading branch information
jwood803 committed Sep 4, 2015
1 parent 9f7517d commit b1aec9c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 14 deletions.
41 changes: 32 additions & 9 deletions ShareExample/Droid/MainActivity.cs
Original file line number Diff line number Diff line change
@@ -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<ImageSource> (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);
}
}
}

21 changes: 21 additions & 0 deletions ShareExample/Droid/Resources/Resource.designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions ShareExample/iOS/AppDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,18 +28,17 @@ 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);

var topController = UIApplication.SharedApplication.KeyWindow.RootViewController;
while (topController.PresentedViewController != null) {
topController = topController.PresentedViewController;
}
topController.PresentViewController (activityController, true, () => {
});
topController.PresentViewController (activityController, true, () => {});
}
}
}

0 comments on commit b1aec9c

Please sign in to comment.