How to Code for Sharing Image and URL in Xamarin Form App Development?

Problem definition:

Recently I faced the problem while working on Xamarin Form App development. Where we have been assigned work to share coupon images on social media like Facebook, Twitter, Facebook Messenger and SMS services.

Xamarin form app development does provide share plugin but this plugin requires an image to be stored on your device itself. So basically the problem is that images are loading directly from Cloud-based storage into a image view. Hence, we have to share the coupon which other users.

Another problem is that Facebook app doesn’t allow to share custom message when sharing image according to a latest Facebook policy. Facebook doesn’t accept EXTRA_PARAMETERS anymore. Here I explained the step wise solution with C# code.

Hire Now!

Do You Need Dedicated Xamarin Developer for Hire?

Hire our best Xamarin developer on hourly, Full-time, Part-time bases. We are ready to help you resolve the problems for Xamarin app development.
Hire Now!

Solution:

Here, I will show you two Method and both are a different way to share.

  1. Share Image via (Facebook, Gmail, Google+, SMS, Twitter etc.)
  2. Share Url Link via (Facebook, Gmail, Google+, SMS, Twitter etc.)

1. Share Image via (Facebook, Gmail, Google+, SMS, Twitter etc.)
If you want to share the only image from Android, ios device using Xamarin Platform. You need to add some permission on Android device.

  • WRITE_EXTERNAL_STORAGE
  • READ_EXTERNAL_STORAGE
  • INTERNET

Now, we are going to implement the coding in C#.
Here is the C# code for sharing Image on Android using Xamarin App Development Environment. 

public void ShareImage() {
var webClient = new WebClient();
var url = new Uri(shareImageUrl);
webClient.DownloadDataCompleted += (d, y) =>{ var bytes = y.Result;
if (bytes != null && bytes.Length > 0){
var sdCardPath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
var tempFilename = “QCopuon.png”;
var filePath = System.IO.Path.Combine(sdCardPath, tempFilename);
File.WriteAllBytes(filePath, bytes);
var pathimage = $”file://{sdCardPath}/{tempFilename}”;
var imageurl= Android.Net.Uri.Parse(pathimage);
ShareImageViaAndroid(imageurl); } };
webClient.DownloadDataAsync(url); }
public void ShareImageViaAndroid(Android.Net.Uri imageUri) { var sharingIntent = new Intent(); sharingIntent.SetAction(Intent.ActionSend);
sharingIntent.SetType(“image/*”);
sharingIntent.PutExtra(Intent.ExtraSubject, “Check out this great deal with jeshal”);
sharingIntent.PutExtra(Intent.ExtraText, “Check out this great deal!”);
sharingIntent.PutExtra(Intent.ExtraStream, imageUri);
sharingIntent.AddFlags(ActivityFlags.GrantReadUriPermission);
Context.StartActivity(Intent.CreateChooser(sharingIntent, “Share Image…”));
}

Here is the C# code for sharing Image on iOS using Xamarin Platform.

public void ShareImageAsyc()
{
string message=”share image demo”;
string Imageurl = “image url”;
var items = new System.Collections.Generic.List{ new NSString(message ?? string.Empty) };
items.Add(new NSString(Imageurl));
var controller = new UIActivityViewController(items.ToArray(), null);
UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(controller, true, null);
}

2. Share Url Link via (Facebook, Gmail, Google+, SMS, Twitter etc.)

In my case we need to share image plus message on facebook and facebook sharing feature does not provide text + image. So that we can find out the solution with sharing Url because of Facebook Graph API automatically detect which URL is shared. Display content of shared Url on facebook profile below some point it really usefully to a developer for sharing image + text.

 

You need to create a web app within my case, I have created shareLink page using Asp.Net MVC.

ViewName:shareLink.csHtml
@model ShareLink Model
@{
Layout = null;
}

Create Model for bind with ShareLink Page

public class ShareImageModel
{
public string imageUrl{ get; set; }
public string Title{ get; set; }
}


Controller Action Method
public ActionResult shareLink(int id){
var model=_dbcontext.ShareImageModel.FindBy(id);
return View(model);
}

 

Now here code for sharing URl on facebook, twitter etc. For facebook share, it’s give user to share link with dyanamic content of page.

public void ShareImageViaAndroid(string title,string UrlLink)
{
var sharingIntent = new Intent();
sharingIntent.SetAction(Intent.ActionSend);
sharingIntent.SetType(“text/plain”);
var items = new System.Collections.Generic.List<string>();
if(title!=null)
items.Add(title +”check out deal!”);

if (!string.IsNullOrEmpty(shareUrl))
items.Add(shareUrl);
sharingIntent.PutExtra(Intent.ExtraText, string.Join(Environment.NewLine, items));
sharingIntent.AddFlags(ActivityFlags.GrantReadUriPermission);
Context.StartActivity(Intent.CreateChooser(sharingIntent, “Share Image.”));
}

Here, I described the iOS code.

public void ShareImageAsyc()
{
if (title != null)
items.Add(new ShareActivityItemSource(new NSString(title), title));
if (url != null)
items.Add(new ShareActivityItemSource(NSUrl.FromString(url), title));

var controller = new UIActivityViewController(items.ToArray(), null);
UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(controller, true, completionHandler: () =>
{
controller.SetCompletionHandler(userHandle);
});
}

So finally, this way you can code for the sharing media and URLs on social media. If you like my help blog then you can share on social for other .Net Developers.

 

By | 2017-09-20T11:16:30+00:00 February 7th, 2017|How To, xamarin development|