×
Namespaces

Variants
Actions

Sharing media on Windows Phone 8

From Nokia Developer Wiki
Jump to: navigation, search

This article explains how to share images from Windows Phone to multiple destinations including cloud storage services, social networks. We will also utilize Bluetooth and NFC to perform easy sharing locally between different devices.

WP Metro Icon Multimedia.png
Article Metadata
Platform Security
Capabilities: ID_CAP_LOCATION, ID_CAP_NETWORKING
Article
Created: MMlejnek (29 Aug 2013)
Last edited: BuildNokia (13 Jun 2014)

Contents

Introduction

This article will help you implement media sharing in your Windows Phone 8 applications. We will cover multiple different techniques and implement them in a way so that they will be easy to plug in into any app you create.

Built-in sharing

Windows Phone offers a powerful image sharing capability built-in. The main advantages of this default approach is that any app can be plugged in to the sharing list, receive the shared image and then share it forward – on social networks, cloud storage, etc.

Implementation

First we implement a simple UI – we need only one button to choose the image from the photos library / camera and an application button to share it.

<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
 
<StackPanel x:Name="TitlePanel" Margin="20">
<TextBlock Text="NOKIA IMAGING WIKI" />
<TextBlock Text="built-in sharing" FontSize="40" />
</StackPanel>
 
<Grid x:Name="ContentPanel" Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Button Click="ChooseImage_Click">Capture / choose image</Button>
<Image x:Name="capturedImage" Grid.Row="1" Margin="30" />
</Grid>
</Grid>
<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar>
<shell:ApplicationBarIconButton IsEnabled="False"
IconUri="Assets/share.png" Click="Share_Click" Text="share" />
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>

The code-behind is also quite simple. First we need to declare two class-level variables – a PhotoChooserTask and a string variable for image's path.

PhotoChooserTask photoChooser;
string currentMediaPath = null;

In the page's constructor we have to initialize the PhotoChooserTask and then add an event handler for the task's completion.

public BuiltInSharing()
{
InitializeComponent();
photoChooser = new PhotoChooserTask();
photoChooser.Completed += photoChooser_Completed;
}

The Choose image button's click handler is very simple – just a call to show the Photo Chooser

private void ChooseImage_Click( object sender, RoutedEventArgs e )
{
photoChooser.Show();
}

The "photoChooser_Completed" event handler makes a few checks and exception handling after the user selects an image. We display it in the dedicated image UI control and also store it's full file name. This will be used later for sharing.

void photoChooser_Completed( object sender, PhotoResult e )
{
if ( e.TaskResult == TaskResult.OK )
{
try
{
BitmapImage image = new BitmapImage();
image.SetSource( e.ChosenPhoto );
 
capturedImage.Source = image;
currentMediaPath = e.OriginalFileName;
( ( ApplicationBarIconButton )ApplicationBar.Buttons[ 0 ] ).IsEnabled = true;
}
catch
{
currentMediaPath = null;
( ( ApplicationBarIconButton )ApplicationBar.Buttons[ 0 ] ).IsEnabled = false;
}
}
else
{
currentMediaPath = null;
( ( ApplicationBarIconButton )ApplicationBar.Buttons[ 0 ] ).IsEnabled = false;
}
}

Now we get to the application bar button click handler. We create a ShareMediaTask and let the OS handle the process.

private void Share_Click( object sender, EventArgs e )
{
if ( currentMediaPath != null )
{
//we can share
ShareMediaTask task = new ShareMediaTask();
task.FilePath = currentMediaPath;
task.Show();
}
}

Disadvantages of ShareMediaTask

As we could see, implementing the ShareMediaTask is very easy and fast. However, it has some disadvantages. Firstly, we don't know which sharing target-capable apps the user has installed so we cannot guarantee that all users will have the same experience. The second major problem is that the ShareMediaTask doesn't support sharing from the app's IsolatedStorage. The reason for this is that the OS would have to give read access to the file in Isolated Storage, which is forbidden. You can overcome this issue by in-memory image rendering using "WriteableBitmap", but this approach is less reliable for larger images and low-memory devices.

Windows Azure Storage

Summary

Remove Category:Draft when the page is complete or near complete


The "platform categories" will be displayed here in preview only - Copy paste relevant categories into text here

Version Hint

Windows Phone: [[Category:Windows Phone]]
[[Category:Windows Phone 7.5]]
[[Category:Windows Phone 8]]

Nokia Asha: [[Category:Nokia Asha]]
[[Category:Nokia Asha Platform 1.0]]

Series 40: [[Category:Series 40]]
[[Category:Series 40 1st Edition]] [[Category:Series 40 2nd Edition]]
[[Category:Series 40 3rd Edition (initial release)]] [[Category:Series 40 3rd Edition FP1]] [[Category:Series 40 3rd Edition FP2]]
[[Category:Series 40 5th Edition (initial release)]] [[Category:Series 40 5th Edition FP1]]
[[Category:Series 40 6th Edition (initial release)]] [[Category:Series 40 6th Edition FP1]] [[Category:Series 40 Developer Platform 1.0]] [[Category:Series 40 Developer Platform 1.1]] [[Category:Series 40 Developer Platform 2.0]]

Symbian: [[Category:Symbian]]
[[Category:S60 1st Edition]] [[Category:S60 2nd Edition (initial release)]] [[Category:S60 2nd Edition FP1]] [[Category:S60 2nd Edition FP2]] [[Category:S60 2nd Edition FP3]]
[[Category:S60 3rd Edition (initial release)]] [[Category:S60 3rd Edition FP1]] [[Category:S60 3rd Edition FP2]]
[[Category:S60 5th Edition]]
[[Category:Symbian^3]] [[Category:Symbian Anna]] [[Category:Nokia Belle]]

Add categories below using category selector.

This page was last modified on 13 June 2014, at 02:55.
100 page views in the last 30 days.
×