Click here to Skip to main content
13,868,550 members
Click here to Skip to main content
Add your own
alternative version


2 bookmarked
Posted 6 Jan 2019
Licenced CPOL

Xamarin Forms: Share Media With Other Apps

, 6 Jan 2019
Rate this:
Please Sign up or sign in to vote.
This article will help you develop a Xamarin.Forms based app to share media files with other mobile applications.


This article provides a solution to share media files from Xamarin.Forms mobile application with other mobile apps.


Before proceeding with this article, please go through some basics of Xamarin.Forms, Dependency Service, Xamarin.Android and Xamarin.iOS. That will help you understand the flow of the project.

Using the Code

Here, we will be using two clients (Android and iOS). 


We'll be using DependencyService to have different implementations for different clients. Here is the interface used.

public interface IShareImage
    Task Share(string url);

In your XAML page, add the following ToolBarItem with a Command associated:

            var share = new ToolbarItem
                Text = "Share",
                Command = new Command(async () =>
                        await DependencyService.Get<IShareImage>().Share("Your Media Link");
                    catch (Exception ex)
                        await DisplayAlert("Error", "Something went wrong. 
                                            Please try again later", "OK");



For Android Implementation, first we need to temporarily save the image on the device, and then we can share it using Intent. To get this flow to work, user needs to allow permissions for the app in the App Info in the mobile device settings.

Temporarily saving the image on the device:

var path = Android.OS.Environment.GetExternalStoragePublicDirectory("Temp");

if (!File.Exists(path.Path))

string absPath = path.Path + "tempfile.jpg";
File.WriteAllBytes(absPath, GetBytes(url));

Now creating an Intent to share the image:

var _context = Android.App.Application.Context;
Intent sendIntent = new Intent(global::Android.Content.Intent.ActionSend);
sendIntent.PutExtra(global::Android.Content.Intent.ExtraText, "Application Name");
sendIntent.PutExtra(Intent.ExtraStream, Android.Net.Uri.Parse("file://" + absPath));
_context.StartActivity(Intent.CreateChooser(sendIntent, "Sharing"));


To get it to work, we need to allow these permissions for our application.

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

File Provider Access

If your targetSdkVersion >= 24, then we have to use FileProvider class to give access to the particular file or folder to make them accessible for other apps.There are different ways of doing it but this is how I've done. Add the following lines in MAINACTIVITY.CS class:

StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();


Here is the IShareImage implementation for Xamarin.iOS.

var imgSource = ImageSource.FromUri(new Uri(url));
var handler = new ImageLoaderSourceHandler();
var uiImage = await handler.LoadImageAsync(imgSource);
var img = NSObject.FromObject(uiImage);
var mess = NSObject.FromObject("App Name");
var activityItems = new[] { mess, img };
var activityController = new UIActivityViewController(activityItems, null);
var topController = UIApplication.SharedApplication.KeyWindow.RootViewController;

while (topController.PresentedViewController != null)
    topController = topController.PresentedViewController;

topController.PresentViewController(activityController, true, () => { });


In case you want to save the media/image to your device, do not forget to set the following Property on Info.plist.

  • Privacy - Photo Library Additions Usage Description

Points of Interest

With minor changes, this code can be reused for different media types like mp3, mp4, documents, pdf, etc. 


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Harshit Gindra
Web Developer
United States United States
I'm an aspiring Developer recently started more and more interested towards ASP.NET and related technologies. I tend to love playing around with different technologies and get more hands on experience by working on different projects. My prime interest lies in Mobile Development(both native and hybrid) and have developed few basic application during my course of studies and work. Always up for a challenge and plan on to finish it through best possible approach.

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.190214.1 | Last Updated 6 Jan 2019
Article Copyright 2019 by Harshit Gindra
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid