How to Share a Media File from WP8 using the ShareMediaTask?


If you are a WP8Dev, 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? WP8 SDK provides you an API by which you can implement the same easily. Today we are going to discuss on this with the API details and integration steps. - Article authored by Kunal Chowdhury on .

If you are a Windows Phone 8 application developer, 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? Windows Phone 8 SDK provides you an API by which you can implement the same easily.

 

Today in this post, we will learn about the API and the implementation steps in a Windows Phone application. If you are looking for it, this will be an ideal solution for you.

 

Implementation Steps

Windows Phone 8 SDK provides you an API named ShareMediaTask, which helps you to implement the media sharing feature in your phone application. The said class is present in the “Microsoft.Phone.Tasks” namespace. ShareMediaTask is actually a launcher which 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 users phone device.

 

If you are looking to share image from picture library or camera, 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, you can set the ShowCamera property to true. For details, check out the below 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;
    photoChooserTask.Show();
}
 
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};
    shareMediaTask.Show();
}

 

As shown in the above code snippet, once the user choose the image from the photo library or the camera, the user will come to the OnPhotoChooserTaskCompleted event. There you can create an instance of the ShareMediaTask and set the FilePath property with the OriginalFileName of the choosen 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).

 

Know more about the API

That was all about the actual implementation steps. If you want to know more about the APIs, continue reading further where I will discuss with you about the meta data of those classes.

 

As shown in the below code snippet, Microsoft.Phone.Tasks.PhotoChooserTask provides a method call Show() which launches the photo chooser application for user interaction. There are three properties named PixelWidth, PixelHeight and ShowCamera which you can use to specify some default values:

 

namespace Microsoft.Phone.Tasks
{
  public sealed class PhotoChooserTask : ChooserBase<PhotoResult>
  {
    [SecuritySafeCritical]
    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 which 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, which allows 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
  {
    [SecuritySafeCritical]
    public void Show();
 
    public string FilePath { get; set; }
  }
}

 

As 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 unauthorised use of the APIs.

 

I hope that the post was useful and if you were looking for the same to integrate in your application, 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. Don’t forget to subscribe to my blog’s RSS feed and Email Newsletter as this will help you to get the updates delivered to your inbox.

Have a question? Or, a comment? Let's Discuss it below...

dhgate

Thank you for visiting our website!

We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.

We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.