How to Share a Media File From WP8 Using the ShareMediaTask

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:

// create an instance of ShareMediaTask and set the FilePath before showing in UIvar shareMediaTask = newShareMediaTask { FilePath = e.OriginalFileName }; shareMediaTask.Show();}

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{publicsealedclassPhotoChooserTask : ChooserBase<PhotoResult> { [SecuritySafeCritical]publicoverridevoid Show(); publicint PixelHeight { get; set; }publicint PixelWidth { get; set; }publicbool 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{publicsealedclassShareMediaTask { [SecuritySafeCritical]publicvoid Show(); publicstring 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.