Virtex Edge Design
  • Home
  • Portfolio
  • Blog
  • Discord
  • About
April 1, 2018 by virtex.edge
Dev Blog, Tutorials

Sharing Screenshots to Social Media in C# and Xamarin

Sharing Screenshots to Social Media in C# and Xamarin
April 1, 2018 by virtex.edge
Dev Blog, Tutorials

Keeping up with the theme of my last post, I’m here today sharing another single class solution to a common stack overflow question; How to  share info (in this case a screenshot) to social media platform on your phone.

For our recent release, Pew Zoom Boom, we wanted a way for people to share scores, beyond just Leaderboards, so we implemented sharing for social media networks. It’s pretty simple to set up and get working in your own project.

Set Up

First you’ll need to save the screenshot to a path that’s accessible from your app. Then call this Method here:

[code language=”javascript”]
///
/// Shares an image using the preferred method in Android or iOS.
///
public void ShareImage(string path, string extratxt = “”)
{

}
[/code]

The path is to the screenshot you’ve taken, and the ‘extratxt’ is for any text you’d like to add to your post.

Sharing Is Caring

Now we need to fill in the empty method above. The Method is split by #if blocks for Android and iOS.

Android

For Android you want to setup a few things first:

  • first set up the share intent,
  • add the ‘extratxt’
  • finally, create a Java File object and then get the uri of the tha Java File image.

[code language=”javascript”]
// Set up the Share Intent
var shareIntent = new Intent(Intent.ActionSend);
// Add Text to go along with it
shareIntent.PutExtra(Intent.ExtraText, extratxt);

// The link to the photo you want to share (i.e the path to the saved screenshot)
Java.IO.File photoFile = new Java.IO.File(path);
var uri = Android.Net.Uri.FromFile(photoFile);
[/code]

Then finally add the uri stream to the shareIntent, Set the Type to Image. You also need to App ReadURI Permissions.

[code language=”javascript”]
// Add the required info the the shareIntent
shareIntent.PutExtra(Intent.ExtraStream, uri);
shareIntent.SetType(“image/*”);
shareIntent.AddFlags(ActivityFlags.GrantReadUriPermission);
[/code]

And then finally launch the intent by calling the below code.

[code language=”javascript”]
// Now Send the Share Intent
Android.App.Application.Context.StartActivity(Intent.CreateChooser(shareIntent, “Choose one”));
[/code]

Calling the last line will start the share intent allowing you to share across different social media platforms:

iOS

iOS requires even less lines of code, but needs a special line if it’s for a tablet opposed to a phone.

[code language=”javascript”]
// Get the ViewController from MonoGame
UIViewController ViewController = Engine.Game.Services.GetService(typeof(UIViewController)) as UIViewController;

// Create an Image Object with the Path to the Screenshot
UIImage image = new UIImage(path);
NSObject[] shareItems = { image };

// Create a View Controller For the Activity
UIActivityViewController shareViewController = new UIActivityViewController(shareItems, null);
shareViewController.ExcludedActivityTypes = new NSString[] { };

// Handle if it’s for an iPad
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
{
shareViewController.PopoverPresentationController.SourceView = ViewController.View;
shareViewController.PopoverPresentationController.SourceRect = new CoreGraphics.CGRect((ViewController.View.Bounds.Width / 2), (ViewController.View.Bounds.Height / 4), 0, 0);
}

// Finally present the Activity
ViewController.PresentViewController(shareViewController, true, null);
[/code]

And that’s all, from there you can share to any social networks you have installed. Through my own testing it’s worked with Twitter, WhatsApp, Facebook, and Sanpchat.

Putting It All Together

We can put both of these blocks of code together into a single method seperated with #if blocks:

[code language=”javascript”]
///
/// Shares an image using the preferred method in Android or iOS.
///
/// Path.
public void ShareImage(string path, string extratxt = “”)
{
#if __ANDROID__
try
{
// Set up the Share Intent
var shareIntent = new Intent(Intent.ActionSend);
// Add Text to go along with it
shareIntent.PutExtra(Intent.ExtraText, extratxt);

// The link to the photo you want to share (i.e the path to the saved screenshot)
Java.IO.File photoFile = new Java.IO.File(path);
var uri = Android.Net.Uri.FromFile(photoFile);

// Add the required info the the shareIntent
shareIntent.PutExtra(Intent.ExtraStream, uri);
shareIntent.SetType(“image/*”);
shareIntent.AddFlags(ActivityFlags.GrantReadUriPermission);

// Now Send the Share Intent
Android.App.Application.Context.StartActivity(Intent.CreateChooser(shareIntent, “Choose one”));
}
catch
{
Console.WriteLine(“Error Sharing Results…”);
}
#elif __IOS__
try
{
//UIViewController ViewController = Engine.Game.Services.GetService(typeof(UIViewController)) as UIViewController;

UIImage image = new UIImage(path);
NSObject[] activityItems = { image };

UIActivityViewController activityViewController = new UIActivityViewController(activityItems, null);
activityViewController.ExcludedActivityTypes = new NSString[] { };

if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
{
activityViewController.PopoverPresentationController.SourceView = ViewController.View;
activityViewController.PopoverPresentationController.SourceRect = new CoreGraphics.CGRect((ViewController.View.Bounds.Width / 2), (ViewController.View.Bounds.Height / 4), 0, 0);
}

ViewController.PresentViewController(activityViewController, true, null);
}
catch
{
Console.WriteLine(“Error Sharing Results…”);
}
#else
Console.WriteLine(“Sharing Not Available On This Platform.”);
#endif
}
[/code]

Pop it into your code and give it a try!

And If you like what you read, give me a follow here or keep up with me on Twitter.

Android C# Game Engines Games Gaming Indie Games iOS MonoGame sharing videogames Visual Studio Xamarin

Previous articleHandling Mobile Ads in C# and XamarinNext article Metric Racer

2 comments

Pingback: [Code Sample/Tutorial] - Sharing in game screenshots to Social Media. - How to Code .NET
R.T. says:
April 2, 2018 at 3:02 am

Reblogged this on rtdev2.

Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

About The Blog

Virtex Edge Design is a a software design company which focuses  Game Development and design of 3D Development Tools.

Recent Posts

Indie Racers 2023!August 30, 2023
V0.9.2 – Cross Platform Track Sharing Out Now + Indie Racers Fest ’23August 30, 2023
INDIE RACERS FESTIVAL 2022!August 26, 2022

Categories

  • Apps
  • Chrome Extensions
  • Design
  • Dev Blog
  • Events
  • Games
  • Metric Racer
  • Preview
  • Shader Series
  • Space Esc8bit
  • The Chaotic Workshop
  • Tutorials
  • Uncategorized
  • Vertices Engine

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Tags

Android blender C# Chrome Coding content generation Design Editor Extensions Extentions Game Design game dev Game Editor Game Engines Games Gaming Graphics HLSL Indie Game Dev Indie Games indie racers indieracers iOS Metric Racer MonoGame Pew Pew Pew Programming Racing racing games Scifi sharing solo SoloDev Solo Indie Dev Steam Steam Workshop Tools Tutorials videogames Visual Studio Web Wipeout Xamarin XNA Youtube

Virtex Edge Design

Virtex Edge Design is an Interactive software design company which focuses on Game Development, immersive experiences and design of 3D Development Tools.

Contact

Victoria, British Columbia
contact@virtexededesign.comvirtexede.design

Recent Posts

Indie Racers 2023!August 30, 2023
V0.9.2 – Cross Platform Track Sharing Out Now + Indie Racers Fest ’23August 30, 2023
INDIE RACERS FESTIVAL 2022!August 26, 2022
Rife Wordpress Theme. Proudly Built By Apollo13

About

Virtex Edge Design is an Interactive software design company which focuses on Game Development, immersive experiences and design of 3D Development Tools.

Recent Posts

Indie Racers 2023!August 30, 2023
V0.9.2 – Cross Platform Track Sharing Out Now + Indie Racers Fest ’23August 30, 2023
INDIE RACERS FESTIVAL 2022!August 26, 2022

Categories

  • Apps
  • Chrome Extensions
  • Design
  • Dev Blog
  • Events
  • Games
  • Metric Racer
  • Preview
  • Shader Series
  • Space Esc8bit
  • The Chaotic Workshop
  • Tutorials
  • Uncategorized
  • Vertices Engine

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org