Reader Level:
Microsoft Phone

How to Share a Media File From WP8 Using the ShareMediaTask

By Kunal Chowdhury on Mar 31 2013
In this article we will learn about the API and the implementation procedure in a Windows Phone application. If that is what you are looking for then this will be an ideal solution for you.
    • Like
    • Love It
    • Awesome
    • Interesting
    • It's Okay
    • Thumbs Down
  • 11.4k
  • 0

If you are a Windows Phone 8 application developer then you might want to implement a functionality to share a media (say, a picture) from your media library or camera library. So, how can you do that? The Windows Phone 8 SDK provides you an API by which you can implement the same easily.

Implementation Steps

The Windows Phone 8 SDK provides you an API named ShareMediaTask, that helps you to implement the media sharing feature in your phone application. The class is present in the "Microsoft.Phone.Tasks" namespace. ShareMediaTask is actually a launcher that actually launches a dialog that allows a user to share a media file on the social networks of their choice. Keep in mind that, those social networks should be integrated in the user's phone device.

If you are looking to share an image from a picture library or camera then you can use the PhotoChooserTask present in the same namespace "Microsoft.Phone.Tasks". As the name says, it's a chooser and allows the user to select a photo by launching the Photo Chooser app.

To begin with this, you need to create an instance of the PhotoChooserTask and then call the Show() method to launch the application. If you want to provide the user access to the camera then you can set the ShowCamera property to true. For details, check out the following code snippet:

private void OnShareMediaTaskClicked(object sender, RoutedEventArgs e)
// create an instance of PhotoChooserTask and specify whether to show Camera
    var photoChooserTask = new PhotoChooserTask { ShowCamera = true };
    photoChooserTask.Completed += OnPhotoChooserTaskCompleted;

void OnPhotoChooserTaskCompleted(object sender, PhotoResult e)
// first unregister the completed event of the PhotoChooserTask
    // this step is recommended to get away from possible memory leak
     var photoChooserTask = (PhotoChooserTask)sender;
     photoChooserTask.Completed -= OnPhotoChooserTaskCompleted;
// create an instance of ShareMediaTask and set the FilePath before showing in UI
     var shareMediaTask = new ShareMediaTask { FilePath = e.OriginalFileName };

As shown in the code snippet above, once the user chooses the image from the photo library or the camera, the user will arrive at the OnPhotoChooserTaskCompleted event. There you can create an instance of the ShareMediaTask and set the FilePath property with the OriginalFileName of the chosen image.

After that, call the Show() method of the ShareMediaTask to launch the share media application and your users will be able to share that image to the networks of their choice (should be integrated in the device).

Learn more about the API

That was all about the actual implementation steps. If you want to learn more about the APIs, continue reading further where I will discuss with you about the meta data of those classes.
As shown in the following code snippet, the "Microsoft.Phone.Tasks.PhotoChooserTask" provides a method call "Show()" that launches the photo chooser application for user interaction. There are three properties named PixelWidth, PixelHeight and ShowCamera that you can use to specify some default values:

namespace Microsoft.Phone.Tasks
public sealed class PhotoChooserTask : ChooserBase<PhotoResult>
public override void Show(); 
public int PixelHeight { get; set; }
public int PixelWidth { get; set; }
public bool ShowCamera { get; set; }

"Microsoft.Phone.Tasks.ShareMediaTask" exposes only a single property named FilePath that will allow the user to set the selected image path. As a developer, you need to assign the selected image here that you will receive via the event argument as shown in the code above. The Show() method launches the share media application of the Windows Phone 8, that allows the user to share the media to the networks of their choice. Here is the meta data of the ShareMediaTask:

namespace Microsoft.Phone.Tasks
public sealed class ShareMediaTask
public void Show(); 
public string FilePath { get; set; }

Since both the classes are sealed, you will not be able to inherit them. Also, there are no APIs to handle these silently. This security measure is there to save the user from unauthorized use of the APIs.

I hope that the article was useful and if you were looking for the same to integrate into your application then you will be able to do so now very easily. Drop a line below if that helps. Also drop a line if you have any further queries. Connect with me on Twitter, Facebook and Google+ to get all technical updates and/or discussions.