private void TakePictureFunction() { if (!mediaPicker.IsCameraAvailable) { alertView = new UIAlertView("MobileTest", "Sorry, you cannot take pictures with your device", new UIAlertViewDelegate(), "OK"); alertView.Show(); return; } mediaPickerController = mediaPicker.GetTakePhotoUI(new StoreCameraMediaOptions { Name = "TakePicture.jpg", Directory = "Images" }); dialogController.PresentViewController(mediaPickerController, true, null); mediaPickerController.GetResultAsync().ContinueWith(t => { dialogController.DismissViewController(true, () => { if (t.IsCanceled || t.IsFaulted) { return; } MediaFile media = t.Result; ShowTakenPicture(media); }); }, uiScheduler); }
private static MediaPickerController SetupController (MediaPickerDelegate mpDelegate, UIImagePickerControllerSourceType sourceType, string mediaType, StoreCameraMediaOptions options = null) { var picker = new MediaPickerController (mpDelegate); picker.MediaTypes = new[] { mediaType }; picker.SourceType = sourceType; if (sourceType == UIImagePickerControllerSourceType.Camera) { picker.CameraDevice = GetUICameraDevice (options.DefaultCamera); if (mediaType == TypeImage) picker.CameraCaptureMode = UIImagePickerControllerCameraCaptureMode.Photo; else if (mediaType == TypeMovie) { StoreVideoOptions voptions = (StoreVideoOptions)options; picker.CameraCaptureMode = UIImagePickerControllerCameraCaptureMode.Video; picker.VideoQuality = GetQuailty (voptions.Quality); picker.VideoMaximumDuration = voptions.DesiredLength.TotalSeconds; } } return picker; }
public override async void ViewWillAppear (bool animated) { base.ViewWillAppear (animated); if (fileName == "") { fileName = "in-progress"; var picker = new MediaPicker (); // new MediaPicker (this); on Android if (!picker.IsCameraAvailable || !picker.PhotosSupported) Console.WriteLine ("No camera!"); else { var options = new StoreCameraMediaOptions { Name = DateTime.Now.ToString("yyyyMMddHHmmss"), Directory = "MediaPickerSample" }; #if !VISUALSTUDIO #region new style pickerController = picker.GetTakePhotoUI (options); PresentViewController (pickerController, true, null); var pickerTask = pickerController.GetResultAsync (); await pickerTask; // We need to dismiss the controller ourselves await DismissViewControllerAsync (true); // woot! async-ified iOS method // User canceled or something went wrong if (pickerTask.IsCanceled || pickerTask.IsFaulted) return; // We get back a MediaFile MediaFile media = pickerTask.Result; fileName = media.Path; PhotoImageView.Image = new UIImage (fileName); SavePicture(fileName); #endregion #else #region old style (deprecated) var t = picker.TakePhotoAsync (options); //.ContinueWith (t => { await t; if (t.IsCanceled) { Console.WriteLine ("User canceled"); fileName = "cancelled"; //InvokeOnMainThread(() =>{ NavigationController.PopToRootViewController(false); //}); return; } Console.WriteLine (t.Result.Path); fileName = t.Result.Path; //InvokeOnMainThread(() =>{ PhotoImageView.Image = new UIImage (fileName); //}); SavePicture(fileName); //}); #endregion #endif } } else if (fileName == "cancelled") { NavigationController.PopToRootViewController (true); } else { // populate screen with existing item PhotoImageView.Image = new UIImage (fileName); LocationText.Text = location; } var locator = new Geolocator { DesiredAccuracy = 50 }; // new Geolocator (this) { ... }; on Android var position = await locator.GetPositionAsync (timeout: 10000); //.ContinueWith (p => { Console.WriteLine ("Position Latitude: {0}", position.Latitude); Console.WriteLine ("Position Longitude: {0}", position.Longitude); location = string.Format("{0},{1}", position.Latitude, position.Longitude); LocationText.Text = location; }
public override async void ViewWillAppear (bool animated) { base.ViewWillAppear (animated); if (fileName == "") { fileName = "in-progress"; var picker = new MediaPicker (); // new MediaPicker (this); on Android if (!picker.IsCameraAvailable || !picker.PhotosSupported) Console.WriteLine ("No camera!"); else { var options = new StoreCameraMediaOptions { Name = DateTime.Now.ToString("yyyyMMddHHmmss"), Directory = "MediaPickerSample" }; pickerController = picker.GetTakePhotoUI (options); PresentViewController (pickerController, true, null); MediaFile media; try { var pickerTask = pickerController.GetResultAsync (); await pickerTask; // User canceled or something went wrong if (pickerTask.IsCanceled || pickerTask.IsFaulted) { fileName = ""; return; } media = pickerTask.Result; fileName = media.Path; // We need to dismiss the controller ourselves await DismissViewControllerAsync (true); // woot! async-ified iOS method } catch(AggregateException ae) { fileName = ""; Console.WriteLine("Error while huh", ae.Message); } catch(Exception e) { fileName = ""; Console.WriteLine("Error while cancelling", e.Message); } if (String.IsNullOrEmpty (fileName)) { await DismissViewControllerAsync (true); } else { PhotoImageView.Image = new UIImage (fileName); SavePicture(fileName); } } } else if (fileName == "cancelled") { NavigationController.PopToRootViewController (true); } else { // populate screen with existing item PhotoImageView.Image = FileExists(fileName) ? new UIImage (fileName) : null; LocationText.Text = location; } var locator = new Geolocator { DesiredAccuracy = 50 }; var position = await locator.GetPositionAsync ( new CancellationToken(), false); Console.WriteLine ("Position Latitude: {0}", position.Latitude); Console.WriteLine ("Position Longitude: {0}", position.Longitude); location = string.Format("{0},{1}", position.Latitude, position.Longitude); LocationText.Text = location; }
public override bool FinishedLaunching (UIApplication app, NSDictionary options) { pickPhoto = new StringElement ("Pick Photo"); pickPhoto.Tapped += () => { mediaPickerController = mediaPicker.GetPickPhotoUI(); dialogController.PresentViewController (mediaPickerController, true, null); mediaPickerController.GetResultAsync().ContinueWith (t => { // We need to dismiss the controller ourselves dialogController.DismissViewController (true, () => { // User canceled or something went wrong if (t.IsCanceled || t.IsFaulted) return; // We get back a MediaFile MediaFile media = t.Result; ShowPhoto (media); }); }, uiScheduler); // Make sure we use the UI thread to show our photo. }; takePhoto = new StringElement ("Take Photo"); takePhoto.Tapped += () => { // Make sure we actually have a camera if (!mediaPicker.IsCameraAvailable) { ShowUnsupported(); return; } // When capturing new media, we can specify it's name and location mediaPickerController = mediaPicker.GetTakePhotoUI (new StoreCameraMediaOptions { Name = "test.jpg", Directory = "MediaPickerSample" }); dialogController.PresentViewController (mediaPickerController, true, null); mediaPickerController.GetResultAsync().ContinueWith (t => { // We need to dismiss the controller ourselves dialogController.DismissViewController (true, () => { // User canceled or something went wrong if (t.IsCanceled || t.IsFaulted) return; // We get back a MediaFile MediaFile media = t.Result; ShowPhoto (media); }); }, uiScheduler); // Make sure we use the UI thread to show our photo. }; takeVideo = new StringElement ("Take Video"); takeVideo.Tapped += () => { // Make sure video is supported and a camera is available if (!mediaPicker.VideosSupported || !mediaPicker.IsCameraAvailable) { ShowUnsupported(); return; } // When capturing video, we can hint at the desired quality and length. // DesiredLength is only a hint, however, and the resulting video may // be longer than desired. mediaPickerController = mediaPicker.GetTakeVideoUI (new StoreVideoOptions { Quality = VideoQuality.Medium, DesiredLength = TimeSpan.FromSeconds (10), Directory = "MediaPickerSample", Name = "test.mp4" }); dialogController.PresentViewController (mediaPickerController, true, null); mediaPickerController.GetResultAsync().ContinueWith (t => { // We need to dismiss the controller ourselves dialogController.DismissViewController (true, () => { // User canceled or something went wrong if (t.IsCanceled || t.IsFaulted) return; // We get back a MediaFile MediaFile media = t.Result; ShowVideo (media); }); }, uiScheduler); // Make sure we use the UI thread to show our video. }; pickVideo = new StringElement ("Pick Video"); pickVideo.Tapped += () => { if (!mediaPicker.VideosSupported) { ShowUnsupported(); return; } mediaPickerController = mediaPicker.GetPickVideoUI(); dialogController.PresentViewController (mediaPickerController, true, null); mediaPickerController.GetResultAsync().ContinueWith (t => { // We need to dismiss the controller ourselves dialogController.DismissViewController (true, () => { // User canceled or something went wrong if (t.IsCanceled || t.IsFaulted) return; // We get back a MediaFile MediaFile media = t.Result; ShowVideo (media); }); }, uiScheduler); // Make sure we use the UI thread to show our video. }; var root = new RootElement("Xamarin.Media Sample") { new Section ("Picking media") { pickPhoto, pickVideo }, new Section ("Capturing media") { takePhoto, takeVideo } }; dialogController = new DisposingMediaViewController (root); viewController = new UINavigationController (dialogController); window = new UIWindow (UIScreen.MainScreen.Bounds); window.RootViewController = viewController; window.MakeKeyAndVisible (); return true; }
public override bool FinishedLaunching (UIApplication app, NSDictionary options) { pickPhoto = new StringElement ("Pick Photo"); pickPhoto.Tapped += () => { mediaPickerController = mediaPicker.GetPickPhotoUI(); dialogController.PresentViewController (mediaPickerController, true, null); mediaPickerController.GetResultAsync().ContinueWith (t => { // We need to dismiss the controller ourselves dialogController.DismissViewController (true, () => { // User canceled or something went wrong if (t.IsCanceled || t.IsFaulted) return; // We get back a MediaFile MediaFile media = t.Result; ShowPhoto (media); }); }, uiScheduler); // Make sure we use the UI thread to show our photo. }; takePhoto = new StringElement ("Take Photo"); takePhoto.Tapped += () => { // Make sure we actually have a camera if (!mediaPicker.IsCameraAvailable) { ShowUnsupported(); return; } // When capturing new media, we can specify it's name and location mediaPickerController = mediaPicker.GetTakePhotoUI (new StoreCameraMediaOptions { Name = "test.jpg", Directory = "MediaPickerSample" }); dialogController.PresentViewController (mediaPickerController, true, null); mediaPickerController.GetResultAsync().ContinueWith (t => { // We need to dismiss the controller ourselves dialogController.DismissViewController (true, () => { // User canceled or something went wrong if (t.IsCanceled || t.IsFaulted) return; // We get back a MediaFile MediaFile media = t.Result; ShowPhoto (media); }); }, uiScheduler); // Make sure we use the UI thread to show our photo. }; takeVideo = new StringElement ("Take Video"); takeVideo.Tapped += () => { // Make sure video is supported and a camera is available if (!mediaPicker.VideosSupported || !mediaPicker.IsCameraAvailable) { ShowUnsupported(); return; } // When capturing video, we can hint at the desired quality and length. // DesiredLength is only a hint, however, and the resulting video may // be longer than desired. mediaPickerController = mediaPicker.GetTakeVideoUI (new StoreVideoOptions { Quality = VideoQuality.Medium, DesiredLength = TimeSpan.FromSeconds (10), Directory = "MediaPickerSample", Name = "test.mp4" }); dialogController.PresentViewController (mediaPickerController, true, null); mediaPickerController.GetResultAsync().ContinueWith (t => { // We need to dismiss the controller ourselves dialogController.DismissViewController (true, () => { // User canceled or something went wrong if (t.IsCanceled || t.IsFaulted) return; // We get back a MediaFile MediaFile media = t.Result; ShowVideo (media); }); }, uiScheduler); // Make sure we use the UI thread to show our video. }; pickVideo = new StringElement ("Pick Video"); pickVideo.Tapped += () => { if (!mediaPicker.VideosSupported) { ShowUnsupported(); return; } mediaPickerController = mediaPicker.GetPickVideoUI(); dialogController.PresentViewController (mediaPickerController, true, null); mediaPickerController.GetResultAsync().ContinueWith (t => { // We need to dismiss the controller ourselves dialogController.DismissViewController (true, () => { // User canceled or something went wrong if (t.IsCanceled || t.IsFaulted) return; // We get back a MediaFile MediaFile media = t.Result; ShowVideo (media); }); }, uiScheduler); // Make sure we use the UI thread to show our video. }; Action pickPhotoAction = async () => { try { var mediaFile = await mediaPicker.PickPhotoAsync (); ShowPhoto (mediaFile); } catch (TaskCanceledException) { } }; Action takePhotoAction = async () => { if (!mediaPicker.VideosSupported || !mediaPicker.IsCameraAvailable) { ShowUnsupported(); return; } try { var mediaFile = await mediaPicker.TakePhotoAsync (new StoreCameraMediaOptions { Name = "test.jpg", Directory = "MediaPickerSample" }); ShowPhoto (mediaFile); } catch (TaskCanceledException) { } }; useActionSheet = new StringElement ("Use Action Sheet"); useActionSheet.Tapped += () => { if (UIDevice.CurrentDevice.CheckSystemVersion (8, 0)) { var alertContoller = UIAlertController.Create ("Show Photo From?", string.Empty, UIAlertControllerStyle.ActionSheet); alertContoller.AddAction (UIAlertAction.Create ("Pick Photo", UIAlertActionStyle.Default, (action) => pickPhotoAction ())); alertContoller.AddAction (UIAlertAction.Create ("Take Photo", UIAlertActionStyle.Default, (action) => takePhotoAction ())); if (UIDevice.CurrentDevice.UserInterfaceIdiom != UIUserInterfaceIdiom.Pad) alertContoller.AddAction (UIAlertAction.Create ("Cancel", UIAlertActionStyle.Cancel, null)); if (alertContoller.PopoverPresentationController != null) { alertContoller.PopoverPresentationController.PermittedArrowDirections = 0; var rect = viewController.View.Bounds; alertContoller.PopoverPresentationController.SourceRect = rect; alertContoller.PopoverPresentationController.SourceView = viewController.View; } viewController.PresentViewController (alertContoller, true, null); } else { var actionSheet = new UIActionSheet("Show Photo From?"); actionSheet.AddButton("Pick Photo"); actionSheet.AddButton("Take Photo"); if (UIDevice.CurrentDevice.UserInterfaceIdiom != UIUserInterfaceIdiom.Pad) { actionSheet.AddButton("Cancel"); actionSheet.CancelButtonIndex = 2; } actionSheet.Clicked += (object sender, UIButtonEventArgs e) => { if (e.ButtonIndex == 0) pickPhotoAction (); if (e.ButtonIndex == 1) takePhotoAction (); }; actionSheet.ShowInView(viewController.View); } }; var root = new RootElement("Xamarin.Media Sample") { new Section ("Picking media") { pickPhoto, pickVideo }, new Section ("Capturing media") { takePhoto, takeVideo }, new Section ("Action Sheets") {useActionSheet} }; dialogController = new DisposingMediaViewController (root); viewController = new UINavigationController (dialogController); window = new UIWindow (UIScreen.MainScreen.Bounds); window.RootViewController = viewController; window.MakeKeyAndVisible (); return true; }