Skip to content

Commit ebf377a

Browse files
author
Charles Petzold
committed
[PhotoPicker dependency service] Full implementations
1 parent 7b39729 commit ebf377a

12 files changed

+174
-23
lines changed

DependencyService/DependencyServiceSample/DependencyServiceSample/DependencyServiceSample.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ public App()
117117
{
118118
pickPictureButton.IsEnabled = false;
119119
Stream stream = await DependencyService.Get<IPicturePicker>().GetImageStreamAsync();
120-
// pickPictureButton.IsEnabled = true;
121120

122121
if (stream != null)
123122
{
@@ -126,13 +125,15 @@ public App()
126125
Source = ImageSource.FromStream(() => stream),
127126
BackgroundColor = Color.Gray
128127
};
128+
129129
TapGestureRecognizer recognizer = new TapGestureRecognizer();
130130
recognizer.Tapped += (sender2, args) =>
131131
{
132132
(MainPage as ContentPage).Content = stack;
133133
pickPictureButton.IsEnabled = true;
134134
};
135135
image.GestureRecognizers.Add(recognizer);
136+
136137
(MainPage as ContentPage).Content = image;
137138
}
138139
else

DependencyService/DependencyServiceSample/Droid/BatteryImplementation.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@
99
[assembly: Xamarin.Forms.Dependency (typeof (BatteryImplementation))]
1010
namespace DependencyServiceSample.Droid
1111
{
12-
1312
public class BatteryImplementation : IBattery
1413
{
15-
1614
public BatteryImplementation ()
1715
{
1816
}

DependencyService/DependencyServiceSample/Droid/DependencyServiceSample.Droid.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
</ItemGroup>
115115
<ItemGroup>
116116
<Compile Include="MainActivity.cs" />
117+
<Compile Include="PicturePickerImplementation.cs" />
117118
<Compile Include="Resources\Resource.designer.cs" />
118119
<Compile Include="Properties\AssemblyInfo.cs" />
119120
<Compile Include="BatteryImplementation.cs" />

DependencyService/DependencyServiceSample/Droid/DeviceOrientationImplementation.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
[assembly: Xamarin.Forms.Dependency (typeof (DeviceOrientationImplementation))]
1212
namespace DependencyServiceSample.Droid
1313
{
14-
1514
public class DeviceOrientationImplementation : IDeviceOrientation
1615
{
1716
public DeviceOrientationImplementation() { }
Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,51 @@
11
using System;
2+
using System.IO;
3+
using System.Threading.Tasks;
24

35
using Android.App;
46
using Android.Content;
57
using Android.Content.PM;
6-
using Android.Runtime;
7-
using Android.Views;
8-
using Android.Widget;
98
using Android.OS;
109

1110
namespace DependencyServiceSample.Droid
1211
{
13-
[Activity (Label = "DependencyServiceSample.Droid", Icon = "@drawable/icon", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
14-
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
15-
{
16-
protected override void OnCreate (Bundle bundle)
17-
{
18-
base.OnCreate (bundle);
19-
20-
global::Xamarin.Forms.Forms.Init (this, bundle);
21-
22-
LoadApplication (new App ());
23-
}
24-
}
12+
[Activity (Label = "DependencyServiceSample.Droid", Icon = "@drawable/icon", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
13+
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity // , IPicturePicker
14+
{
15+
protected override void OnCreate (Bundle bundle)
16+
{
17+
base.OnCreate (bundle);
18+
19+
global::Xamarin.Forms.Forms.Init (this, bundle);
20+
21+
LoadApplication (new App ());
22+
}
23+
24+
// Field, property, and method for Picture Picker
25+
public static readonly int PickImageId = 1000;
26+
27+
public TaskCompletionSource<Stream> PickImageTaskCompletionSource { set; get; }
28+
29+
protected override void OnActivityResult(int requestCode, Result resultCode, Intent intent)
30+
{
31+
base.OnActivityResult(requestCode, resultCode, intent);
32+
33+
if (requestCode == PickImageId)
34+
{
35+
if ((resultCode == Result.Ok) && (intent != null))
36+
{
37+
Android.Net.Uri uri = intent.Data;
38+
Stream stream = ContentResolver.OpenInputStream(uri);
39+
40+
// Set the Stream as the completion of the Task
41+
PickImageTaskCompletionSource.SetResult(stream);
42+
}
43+
else
44+
{
45+
PickImageTaskCompletionSource.SetResult(null);
46+
}
47+
}
48+
}
49+
}
2550
}
2651

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.IO;
3+
using System.Threading.Tasks;
4+
5+
using Android.Content;
6+
7+
using Xamarin.Forms;
8+
9+
using DependencyServiceSample.Droid;
10+
11+
[assembly: Dependency(typeof(PicturePickerImplementation))]
12+
13+
namespace DependencyServiceSample.Droid
14+
{
15+
public class PicturePickerImplementation : IPicturePicker
16+
{
17+
public Task<Stream> GetImageStreamAsync()
18+
{
19+
// Define the Intent for getting images
20+
Intent intent = new Intent();
21+
intent.SetType("image/*");
22+
intent.SetAction(Intent.ActionGetContent);
23+
24+
// Get the MainActivity instance
25+
MainActivity activity = Forms.Context as MainActivity;
26+
27+
// Start the picture-picker activity (resumes in MainActivity.cs)
28+
activity.StartActivityForResult(
29+
Intent.CreateChooser(intent, "Select Picture"),
30+
MainActivity.PickImageId);
31+
32+
// Save the TaskCompletionSource object as a MainActivity property
33+
activity.PickImageTaskCompletionSource = new TaskCompletionSource<Stream>();
34+
35+
// Return Task object
36+
return activity.PickImageTaskCompletionSource.Task;
37+
}
38+
}
39+
}

DependencyService/DependencyServiceSample/Metadata.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
<SupportedPlatforms>iOS, Android, Windows</SupportedPlatforms>
88
<LicenseRequirement>Indie</LicenseRequirement>
99
<Gallery>false</Gallery>
10-
<Brief>This sample demonstrates how to use the DependencyService to implement text-to-speech, check device orientation, and check battery status.</Brief>
10+
<Brief>This sample demonstrates how to use the DependencyService to implement text-to-speech, check device orientation, check battery status, and pick a photo from the phone's photo library.</Brief>
1111
</SampleMetadata>
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
Dependency Service
22
==================
33

4-
This sample demonstrates how to use the `DependencyService` to implement text-to-speech, check device orientation, and check battery status.
4+
This sample demonstrates how to use the `DependencyService` to implement text-to-speech, check device orientation, battery status, and select a photo from the phone's photo library.
55

6-
For more information about this sample see [Accessing Native Features with DependencyService](http://developer.xamarin.com/guides/cross-platform/xamarin-forms/dependency-service/).
6+
For more information about this sample see [Accessing Native Features with DependencyService](http://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/dependency-service/).
77

88
Author
99
------
1010

11-
Nathan Castle
11+
Nathan Castle, Charles Petzold

DependencyService/DependencyServiceSample/UWP/DependencyServiceSample.UWP.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
<Compile Include="MainPage.xaml.cs">
101101
<DependentUpon>MainPage.xaml</DependentUpon>
102102
</Compile>
103+
<Compile Include="PicturePickerImplementation.cs" />
103104
<Compile Include="Properties\AssemblyInfo.cs" />
104105
<Compile Include="TextToSpeech.cs" />
105106
</ItemGroup>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.IO;
3+
using System.Threading.Tasks;
4+
5+
using Windows.Storage;
6+
using Windows.Storage.Pickers;
7+
using Windows.Storage.Streams;
8+
9+
using Xamarin.Forms;
10+
11+
using DependencyServiceSample.UWP;
12+
13+
[assembly: Dependency(typeof(PicturePickerImplementation))]
14+
15+
namespace DependencyServiceSample.UWP
16+
{
17+
public class PicturePickerImplementation : IPicturePicker
18+
{
19+
public async Task<Stream> GetImageStreamAsync()
20+
{
21+
// Create and initialize the FileOpenPicker
22+
FileOpenPicker openPicker = new FileOpenPicker
23+
{
24+
ViewMode = PickerViewMode.Thumbnail,
25+
SuggestedStartLocation = PickerLocationId.PicturesLibrary,
26+
};
27+
28+
openPicker.FileTypeFilter.Add(".jpg");
29+
openPicker.FileTypeFilter.Add(".jpeg");
30+
openPicker.FileTypeFilter.Add(".png");
31+
32+
// Get a file and return a Stream
33+
StorageFile storageFile = await openPicker.PickSingleFileAsync();
34+
35+
if (storageFile == null)
36+
{
37+
return null;
38+
}
39+
40+
IRandomAccessStreamWithContentType raStream = await storageFile.OpenReadAsync();
41+
return raStream.AsStreamForRead();
42+
}
43+
}
44+
}

DependencyService/DependencyServiceSample/WinPhone/DependencyServiceSample.WinPhone81.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
<Compile Include="MainPage.xaml.cs">
8989
<DependentUpon>MainPage.xaml</DependentUpon>
9090
</Compile>
91+
<Compile Include="PicturePickerImplementation.cs" />
9192
<Compile Include="Properties\AssemblyInfo.cs" />
9293
<Compile Include="TextToSpeech.cs" />
9394
</ItemGroup>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.IO;
3+
using System.Threading.Tasks;
4+
5+
using Windows.Storage;
6+
using Windows.Storage.Pickers;
7+
using Windows.Storage.Streams;
8+
using Windows.UI.Xaml;
9+
10+
using DependencyServiceSample.WinPhone;
11+
12+
[assembly: Xamarin.Forms.Dependency(typeof(PicturePickerImplementation))]
13+
14+
namespace DependencyServiceSample.WinPhone
15+
{
16+
public class PicturePickerImplementation : IPicturePicker
17+
{
18+
public Task<Stream> GetImageStreamAsync()
19+
{
20+
// Create and initialize the FileOpenPicker
21+
FileOpenPicker openPicker = new FileOpenPicker
22+
{
23+
ViewMode = PickerViewMode.Thumbnail,
24+
SuggestedStartLocation = PickerLocationId.PicturesLibrary,
25+
};
26+
27+
openPicker.FileTypeFilter.Add(".jpg");
28+
openPicker.FileTypeFilter.Add(".jpeg");
29+
openPicker.FileTypeFilter.Add(".png");
30+
31+
// Display the picker for a single file (resumes in OnActivated in App.xaml.cs)
32+
openPicker.PickSingleFileAndContinue();
33+
34+
// Create a TaskCompletionSource stored in App.xaml.cs
35+
TaskCompletionSource<Stream> taskCompletionSource = new TaskCompletionSource<Stream>();
36+
(Application.Current as App).TaskCompletionSource = taskCompletionSource;
37+
38+
// Return the Task object
39+
return taskCompletionSource.Task;
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)