private void TakeAPhoto() { if (this.ModalViewController != null) { this.PerformSelector (new Selector ("TakeAPhoto"), null, 1.0f); return; } var picker = new MediaPicker (); if (picker.IsCameraAvailable) { picker.TakePhotoAsync (new StoreCameraMediaOptions ()) .ContinueWith (t => ProcessImage (t)); } else { picker.PickPhotoAsync ().ContinueWith (t => ProcessImage (t)); } /* imagePicker = new UIImagePickerController (); imagePicker.SourceType = UIImagePickerController.IsSourceTypeAvailable (UIImagePickerControllerSourceType.Camera) ? UIImagePickerControllerSourceType.Camera : UIImagePickerControllerSourceType.PhotoLibrary; imagePicker.FinishedPickingMedia += (sender, e) => { imageView.Image = e.OriginalImage; UIApplication.SharedApplication.NetworkActivityIndicatorVisible = true; ThreadPool.QueueUserWorkItem (callback => { UploadImage (e.OriginalImage); }); this.DismissModalViewControllerAnimated (true); }; imagePicker.Canceled += (sender, e) => { this.DismissModalViewControllerAnimated (true); }; this.PresentModalViewController (imagePicker, true);*/ }
void MediaChooser (object sender, EventArgs e) { var builder = new AlertDialog.Builder(this); builder.SetTitle("Media Chooser"); builder.SetMessage("Camera Or Library"); builder.SetCancelable(false); builder.SetPositiveButton("Camera" , delegate { var picker = new MediaPicker (this); if (!picker.IsCameraAvailable) Console.WriteLine ("No camera!"); else { picker.TakePhotoAsync (new StoreCameraMediaOptions { Name = "test.jpg", Directory = "MediaPickerSample" }).ContinueWith (t => { if (t.IsCanceled) { Console.WriteLine ("User canceled"); return; } Console.WriteLine (t.Result.Path); //Load image to imageView b = BitmapFactory.DecodeFile(t.Result.Path); imageView.SetImageBitmap(b); }, TaskScheduler.FromCurrentSynchronizationContext()); } } ); builder.SetNeutralButton("Library" , delegate { var picker = new MediaPicker (this); picker.PickPhotoAsync() .ContinueWith (t => { if (t.IsCanceled) { Console.WriteLine ("User canceled"); return; } Console.WriteLine (t.Result.Path); b = BitmapFactory.DecodeFile(t.Result.Path); imageView.SetImageBitmap(b); }, TaskScheduler.FromCurrentSynchronizationContext()); } ); builder.SetNegativeButton("Cancel",delegate {}); builder.Show(); }
private void PickPhotoFromLibrary(MediaPicker picker) { Task<MediaFile> task = picker.PickPhotoAsync (); HandlePick (task); }
private static async Task<ImageSource> GetImageFromExistingAsync(ImageOptions options = null) { var mediaPicker = new MediaPicker(); if (mediaPicker.PhotosSupported) { try { var style = UIApplication.SharedApplication.StatusBarStyle; var file = await mediaPicker.PickPhotoAsync(); UIApplication.SharedApplication.StatusBarStyle = style; return ProcessImageFile(options, file); } catch (Exception) { return null; } } return null; }
public async override void ViewDidLoad() { base.ViewDidLoad(); if(id > 0) { BTProgressHUD.Show("Loading..."); await viewModel.Init(id); BTProgressHUD.Dismiss(); } var delete = UIButton.FromType(UIButtonType.RoundedRect); delete.SetTitle("Delete", UIControlState.Normal); delete.Frame = new System.Drawing.RectangleF(0, 0, 320, 44); delete.TouchUpInside += async (sender, e) => { await viewModel.ExecuteDeleteMovieCommand(viewModel.Id); NavigationController.PopToRootViewController(true); }; var save = UIButton.FromType(UIButtonType.RoundedRect); save.SetTitle("Save", UIControlState.Normal); save.Frame = new System.Drawing.RectangleF(0, 0, 320, 44); save.TouchUpInside += async (sender, e) => { viewModel.IsFavorite = isFavorite.Value; await viewModel.ExecuteSaveMovieCommand(); NavigationController.PopToRootViewController(true); }; this.Root = new RootElement(viewModel.Title) { new Section("Movie Details") { (title = new StringElement("Title")), (imdbId = new HtmlElement("Imdb", string.Empty)), (runtime = new StringElement("Runtime")), (releaseDate = new StringElement("Release Date")), (isFavorite = new BooleanElement("Favorite?", false)), }, new Section() { delete, save } }; title.Value = viewModel.Title; imdbId.Url = string.Format("http://m.imdb.com/title/{0}", viewModel.ImdbId); runtime.Value = viewModel.Runtime.ToString(); releaseDate.Value = viewModel.ReleaseDate.ToShortDateString(); isFavorite.Value = viewModel.IsFavorite; this.NavigationItem.RightBarButtonItem = new UIBarButtonItem(UIBarButtonSystemItem.Camera, async delegate { var picker = new Xamarin.Media.MediaPicker(); if (picker.PhotosSupported) { try { MediaFile file = await picker.PickPhotoAsync(); if (file != null) { viewModel.Image = file.GetStream(); await viewModel.ExecuteSaveMovieCommand(); NavigationController.PopToRootViewController(true); } } catch { } } }); }
protected override void OnCreate (Bundle bundle) { base.OnCreate (bundle); SetContentView (Resource.Layout.Main); Button videoButton = FindViewById<Button> (Resource.Id.takeVideoButton); videoButton.Click += delegate { // The MediaPicker is the class used to invoke the // camera and gallery picker for selecting and // taking photos and videos var picker = new MediaPicker (this); // We can check to make sure the device has a camera // and supports dealing with video. if (!picker.IsCameraAvailable || !picker.VideosSupported) { ShowUnsupported(); return; } // TakeVideoAsync is an async API that takes a // StoreVideoOptions object with various // properties, such as the name and folder to // store the resulting video. You can // also limit the length of the video picker.TakeVideoAsync (new StoreVideoOptions { Name = "MyVideo", Directory = "MyVideos", DesiredLength = TimeSpan.FromSeconds (10) }) .ContinueWith (t => { if (t.IsCanceled) return; RunOnUiThread (() => ShowVideo (t.Result.Path)); }); }; Button photoButton = FindViewById<Button> (Resource.Id.takePhotoButton); photoButton.Click += delegate { var picker = new MediaPicker (this); if (!picker.IsCameraAvailable || !picker.PhotosSupported) { ShowUnsupported(); return; } picker.TakePhotoAsync (new StoreCameraMediaOptions { Name = "test.jpg", Directory = "MediaPickerSample" }) .ContinueWith (t => { if (t.IsCanceled) return; RunOnUiThread (() => ShowImage (t.Result.Path)); }); }; Button pickVideoButton = FindViewById<Button> (Resource.Id.pickVideoButton); pickVideoButton.Click += delegate { // The MediaPicker is the class used to invoke the camera // and gallery picker for selecting and taking photos // and videos var picker = new MediaPicker (this); if (!picker.VideosSupported) { ShowUnsupported(); return; } // PickVideoAsync is an async API that invokes // the native gallery picker.PickVideoAsync ().ContinueWith (t => { if (t.IsCanceled) return; RunOnUiThread (() => ShowVideo (t.Result.Path)); }); }; Button pickPhotoButton = FindViewById<Button> (Resource.Id.pickPhotoButton); pickPhotoButton.Click += delegate { var picker = new MediaPicker (this); if (!picker.PhotosSupported) { ShowUnsupported(); return; } picker.PickPhotoAsync ().ContinueWith (t => { if (t.IsCanceled) return; RunOnUiThread (() => ShowImage (t.Result.Path)); }); }; }
async void OpenPicker(){ //It works? Don't use gallery MediaPicker imagePicker = new MediaPicker(Forms.Context); try{ Console.WriteLine(".25: "); MediaFile robotImagePath = await imagePicker.PickPhotoAsync(); //Console.WriteLine(robotImagePath.Path); ParseFile temp = new ParseFile(data["teamNumber"].ToString()+".jpg", ImageToBinary(robotImagePath.Path)); data["robotImage"] = temp; SaveData(); await Navigation.PushModalAsync(new AddPitTeam(data)); } catch{ Console.WriteLine ("Error"); } }
/// <summary> /// Event handler when the user clicks the Pick a Photo button /// </summary> /// <param name='sender'> /// Sender /// </param> partial void pickPhotoBtnClicked (MonoTouch.Foundation.NSObject sender) { Console.WriteLine("pickPhotoBtnClicked"); picker = new MediaPicker (); // Check if photos are supported on this device if (!picker.PhotosSupported) { ShowUnsupported(); return; } // Call PickPhotoAsync, which gives us a Task<MediaFile> picker.PickPhotoAsync () .ContinueWith (t => // Continue when the user has picked a photo { if (t.IsCanceled) // The user canceled return; // Show the photo the user selected InvokeOnMainThread( delegate { UIImage image = UIImage.FromFile(t.Result.Path); this.imageView.Image = image; }); }); }
public static async Task<string> PathToPictureFromGallery (MediaPicker mediaPicker) { try{ if (!mediaPicker.PhotosSupported) { return null; } var mediaFile = await mediaPicker.PickPhotoAsync (); return mediaFile.Path; } catch { return null; } }
public Task<MediaFile> GetPhoto(bool takeNew) { #if MONOANDROID var activity = this.GetService<Cirrious.MvvmCross.Droid.Interfaces.IMvxAndroidCurrentTopActivity>(); var picker = new MediaPicker(activity.Activity); #else var picker = new MediaPicker(); #endif if (takeNew) return picker.TakePhotoAsync(new StoreCameraMediaOptions()); else return picker.PickPhotoAsync(); }
public static void PictureFromGallery (MediaPicker picker, Action<MediaFile> callback) { if (!picker.PhotosSupported) { //ToDo: How to handle unsupported return; } picker.PickPhotoAsync().ContinueWith (t => { if(t.IsCanceled) return; callback(t.Result); }); }
private async void ButtonSendAttachmentsEmail_TouchUpInside(object o, EventArgs eventArgs) { var mediaPicker = new MediaPicker(); MediaFile file = await mediaPicker.PickPhotoAsync(); if (file != null) { var fileName = System.IO.Path.GetFileName(file.Path); // Assume image content is default jpeg var email = SamplesExtensions.BuildSampleEmail() .WithAttachment(fileName, file.GetStream(), "image/jpeg") .Build(); MessagingPlugin.EmailMessenger.SendSampleEmail(email); } }
public async Task<Stream> PickPhoto() { var picker = new MediaPicker(); MediaFile mf = null; try { mf = await picker.PickPhotoAsync(); } catch (TaskCanceledException) { Debug.WriteLine("photo picker canceled"); } if (mf == null) { return null; } return mf.GetStream(); }
public WelcomeViewModel(MediaPicker picker) { TakeNewPhoto = new ReactiveAsyncCommand(); ChooseExistingPhoto = new ReactiveAsyncCommand(); StartEdit = new ReactiveCommand(); var defaultCamera = new StoreCameraMediaOptions() { DefaultCamera = CameraDevice.Rear, Directory = "LeeMe", Name = "turnt.jpg", }; Observable.Merge( TakeNewPhoto.RegisterAsyncTask(_ => picker.TakePhotoAsync(defaultCamera)), ChooseExistingPhoto.RegisterAsyncTask(_ => picker.PickPhotoAsync())) .Select(x => x.Path) .InvokeCommand(StartEdit); // TODO: Write a proper UserError handler for this TakeNewPhoto.ThrownExceptions.Subscribe(ex => Console.WriteLine(ex)); ChooseExistingPhoto.ThrownExceptions.Subscribe(ex => Console.WriteLine(ex)); }
public PhotoAlertSheet () { picker = new MediaPicker (); AddButton ("Photo Library"); if (picker.IsCameraAvailable) AddButton ("Camera"); Dismissed += (sender, e) => { switch (e.ButtonIndex) { case 0: picker.PickPhotoAsync () .ContinueWith (t => { if (t.IsCanceled) return null; SetImage(t.Result.GetStream()); return t.Result; }); break; case 1: picker.TakePhotoAsync (new StoreCameraMediaOptions()) .ContinueWith (t => { if (t.IsCanceled) return t.Result; SetImage(t.Result.GetStream()); return t.Result; }); break; default: break; } }; }
public ExpenseViewModel () { picker = new MediaPicker (); assignmentViewModel = ServiceContainer.Resolve<AssignmentViewModel>(); addExpenseCommand = new DelegateCommand (async obj => { var expense = obj as Expense; if (expense != null) { SelectedExpense = expense; CanDelete = true; AddExpenseHeader = "Expense"; await LoadPhotoAsync (expense); } else { SelectedExpense = new Expense (); CanDelete = false; AddExpenseHeader = "Add Expense"; Photo = new ExpensePhoto (); } if (addExpensePopUp != null && addExpensePopUp.IsOpen) { addExpensePopUp.IsOpen = false; } addExpensePopUp = new Popup (); addExpensePopUp.Height = Window.Current.Bounds.Height; addExpensePopUp.Width = Constants.PopUpWidth; AddExpenseFlyoutPanel flyoutpanel = new AddExpenseFlyoutPanel (); flyoutpanel.Width = addExpensePopUp.Width; flyoutpanel.Height = addExpensePopUp.Height; addExpensePopUp.Child = flyoutpanel; addExpensePopUp.SetValue (Canvas.LeftProperty, Window.Current.Bounds.Width - Constants.PopUpWidth); addExpensePopUp.SetValue (Canvas.TopProperty, 0); addExpensePopUp.IsOpen = true; }); saveExpenseCommand = new DelegateCommand (async _ => { selectedExpense.Cost = ExpenseCost.ToDecimal (CultureInfo.InvariantCulture); selectedExpense.AssignmentId = assignmentViewModel.SelectedAssignment.Id; var task = SaveExpenseAsync (assignmentViewModel.SelectedAssignment, SelectedExpense); if (Photo !=null && Photo.Image != null) { task = task.ContinueWith (obj => { Photo.ExpenseId = SelectedExpense.Id; }); await SavePhotoAsync (); } await LoadExpensesAsync (assignmentViewModel.SelectedAssignment); addExpensePopUp.IsOpen = false; }); deleteExpenseCommand = new DelegateCommand (async _ => { await DeleteExpenseAsync (assignmentViewModel.SelectedAssignment, selectedExpense); await LoadExpensesAsync (assignmentViewModel.SelectedAssignment); addExpensePopUp.IsOpen = false; }); cancelExpenseCommand = new DelegateCommand (_ => { addExpensePopUp.IsOpen = false; }); addImageCommand = new DelegateCommand (async _ => { bool cameraCommand = false, imageCommand = false; var dialog = new MessageDialog ("Take picture with your built in camera or select one from your photo library.", "Add Image"); if (picker.IsCameraAvailable) { dialog.Commands.Add (new UICommand ("Camera", new UICommandInvokedHandler (q => cameraCommand = true))); } dialog.Commands.Add (new UICommand ("Library", new UICommandInvokedHandler (q => imageCommand = true))); await dialog.ShowAsync (); if (cameraCommand) { try { var mediaFile = await picker.TakePhotoAsync (new StoreCameraMediaOptions ()); var photo = await mediaFile.GetStream ().LoadBytes (); if (Photo == null) Photo = new ExpensePhoto { ExpenseId = SelectedExpense.Id }; Photo.Image = photo; OnPropertyChanged ("Photo"); } catch (Exception exc) { Debug.WriteLine (exc.Message); //this could happen if they cancel, etc. } } else if (imageCommand) { try { var mediaFile = await picker.PickPhotoAsync (); var photo = await mediaFile.GetStream ().LoadBytes (); if (Photo == null) Photo = new ExpensePhoto { ExpenseId = SelectedExpense.Id }; Photo.Image = photo; OnPropertyChanged ("Photo"); } catch (Exception exc) { Debug.WriteLine (exc.Message); //this could happen if they cancel, etc. } } }); }
public Task<MediaFile> GetPhoto(bool takeNew, Context context) { var picker = new MediaPicker(context); return takeNew ? picker.TakePhotoAsync(new StoreCameraMediaOptions()) : picker.PickPhotoAsync(); }
private void TakePhoto() { var picker = new MediaPicker (this); if (picker.IsCameraAvailable) { picker.TakePhotoAsync (new StoreCameraMediaOptions ()) .ContinueWith (r => ProcessImage (r)); } else { picker.PickPhotoAsync ().ContinueWith (r => ProcessImage (r)); } }
private void UploadFoto () { try { MediaPicker mediaPicker = new MediaPicker (); mediaPicker.PickPhotoAsync().ContinueWith( t => { MediaFile file = t.Result; CallbackPhotoMade(t.Result); }, TaskScheduler.FromCurrentSynchronizationContext()); } catch (Exception ex) { Insights.Report (ex); } }
protected override void OnCreate (Bundle bundle) { base.OnCreate (bundle); SetContentView (Resource.Layout.Main); ImageView image = FindViewById<ImageView> (Resource.Id.image); VideoView videoView = FindViewById<VideoView>(Resource.Id.surfacevideoview); // // Wire up the take a video button // Button videoButton = FindViewById<Button> (Resource.Id.takeVideoButton); videoButton.Click += delegate { // // The MediaPicker is the class used to // invoke the camera and gallery picker // for selecting and taking photos // and videos // var picker = new MediaPicker (this); // We can check to make sure the device has a camera // and supports dealing with video. if (!picker.IsCameraAvailable || !picker.VideosSupported) { ShowUnsupported(); return; } // // TakeVideoAsync is an async API that takes a // StoreVideoOptions object with various // properties, such as the name and folder to // store the resulting video. You can // also limit the length of the video // picker.TakeVideoAsync (new StoreVideoOptions { Name = "MyVideo", Directory = "MyVideos", DesiredLength = TimeSpan.FromSeconds (10) }) .ContinueWith (t => { if (t.IsCanceled) return; // // Because TakeVideoAsync returns a Task // we can use ContinueWith to run more code // after the user finishes recording the video // RunOnUiThread (() => { // // Toggle the visibility of the image and videoviews // image.Visibility = Android.Views.ViewStates.Gone; videoView.Visibility = Android.Views.ViewStates.Visible; // // Load in the video file // videoView.SetVideoPath(t.Result.Path); // // optional: Handle when the video finishes playing // //videoView.setOnCompletionListener(this); // // Start playing the video // videoView.Start(); }); }); }; // // Wire up the take a photo button // Button photoButton = FindViewById<Button> (Resource.Id.takePhotoButton); photoButton.Click += delegate { var picker = new MediaPicker (this); if (!picker.IsCameraAvailable || !picker.PhotosSupported) { ShowUnsupported(); return; } picker.TakePhotoAsync (new StoreCameraMediaOptions { Name = "test.jpg", Directory = "MediaPickerSample" }) .ContinueWith (t => { if (t.IsCanceled) return; Bitmap b = BitmapFactory.DecodeFile (t.Result.Path); RunOnUiThread (() => { // // Toggle the visibility of the image and video views // videoView.Visibility = Android.Views.ViewStates.Gone; image.Visibility = Android.Views.ViewStates.Visible; // // Display the bitmap // image.SetImageBitmap (b); // Cleanup any resources held by the MediaFile instance t.Result.Dispose(); }); }); }; // // Wire up the pick a video button // Button pickVideoButton = FindViewById<Button> (Resource.Id.pickVideoButton); pickVideoButton.Click += delegate { // // The MediaPicker is the class used to // invoke the camera and gallery picker // for selecting and taking photos // and videos // var picker = new MediaPicker (this); if (!picker.VideosSupported) { ShowUnsupported(); return; } // // PickVideoAsync is an async API that invokes // the native gallery // picker.PickVideoAsync () .ContinueWith (t => { if (t.IsCanceled) return; // // Because PickVideoAsync returns a Task // we can use ContinueWith to run more code // after the user finishes recording the video // RunOnUiThread (() => { // // Toggle the visibility of the image and video views // image.Visibility = Android.Views.ViewStates.Gone; videoView.Visibility = Android.Views.ViewStates.Visible; // // Load in the video file // videoView.SetVideoPath(t.Result.Path); // // Optional: Handle when the video finishes playing // //videoView.setOnCompletionListener(this); // // Start playing the video // videoView.Start(); // Cleanup any resources held by the MediaFile instance t.Result.Dispose(); }); }); }; // // Wire up the pick a photo button // Button pickPhotoButton = FindViewById<Button> (Resource.Id.pickPhotoButton); pickPhotoButton.Click += delegate { var picker = new MediaPicker (this); if (!picker.PhotosSupported) { ShowUnsupported(); return; } picker.PickPhotoAsync () .ContinueWith (t => { if (t.IsCanceled) return; Bitmap b = BitmapFactory.DecodeFile (t.Result.Path); RunOnUiThread (() => { // // Toggle the visibility of the image and video views // videoView.Visibility = Android.Views.ViewStates.Gone; image.Visibility = Android.Views.ViewStates.Visible; // // Display the bitmap // image.SetImageBitmap (b); // Cleanup any resources held by the MediaFile instance t.Result.Dispose(); }); }); }; }
void MediaChooser (object sender, UIButtonEventArgs e) { if (e.ButtonIndex == 1) { System.Console.WriteLine ("Camera"); var picker = new MediaPicker (); if (!picker.IsCameraAvailable) Console.WriteLine ("No camera!"); else { picker.TakePhotoAsync (new StoreCameraMediaOptions { Name = "test.jpg", Directory = "MediaPickerSample" }).ContinueWith (t => { if (t.IsCanceled) { Console.WriteLine ("User canceled"); return; } Console.WriteLine (t.Result.Path); imageView.Image = new UIImage(t.Result.Path); }, TaskScheduler.FromCurrentSynchronizationContext()); } } else if (e.ButtonIndex == 2) { System.Console.WriteLine ("Library"); var picker = new MediaPicker (); // new MediaPicker (this); on Android picker.PickPhotoAsync() .ContinueWith (t => { if (t.IsCanceled) { Console.WriteLine ("User canceled"); return; } Console.WriteLine (t.Result.Path); imageView.Image = new UIImage(t.Result.Path); }, TaskScheduler.FromCurrentSynchronizationContext()); } }
public override bool FinishedLaunching (UIApplication app, NSDictionary options) { var root = new RootElement ("Xamarin.Social Sample"); var section = new Section ("Services"); var facebookButton = new StringElement ("Share with Facebook"); facebookButton.Tapped += delegate { Share (Facebook, facebookButton); }; section.Add (facebookButton); var twitterButton = new StringElement ("Share with Twitter"); twitterButton.Tapped += delegate { Share (Twitter, twitterButton); }; section.Add (twitterButton); var twitter5Button = new StringElement ("Share with built-in Twitter"); twitter5Button.Tapped += delegate { Share (Twitter5, twitter5Button); }; section.Add (twitter5Button); var flickr = new StringElement ("Share with Flickr"); flickr.Tapped += () => { var picker = new MediaPicker(); // Set breakpoint here picker.PickPhotoAsync().ContinueWith (t => { if (t.IsCanceled) return; var item = new Item ("I'm sharing great things using Xamarin!") { Images = new[] { new ImageData (t.Result.Path) } }; Console.WriteLine ("Picked image {0}", t.Result.Path); UIViewController viewController = Flickr.GetShareUI (item, shareResult => { dialog.DismissViewController (true, null); flickr.GetActiveCell().TextLabel.Text = "Flickr shared: " + shareResult; }); dialog.PresentViewController (viewController, true, null); }, TaskScheduler.FromCurrentSynchronizationContext()); }; section.Add (flickr); root.Add (section); dialog = new DialogViewController (root); window = new UIWindow (UIScreen.MainScreen.Bounds); window.RootViewController = new UINavigationController (dialog); window.MakeKeyAndVisible (); return true; }
protected override void OnCreate (Bundle bundle) { base.OnCreate (bundle); SetContentView (Resource.Layout.Main); Button flickrButton = FindViewById<Button> (Resource.Id.Flickr); flickrButton.Click += (sender, args) => { var picker = new MediaPicker (this); picker.PickPhotoAsync().ContinueWith (t => { if (t.IsCanceled) return; var item = new Item ("I'm sharing great things using Xamarin!") { Images = new[] { new ImageData (t.Result.Path) } }; Intent intent = Flickr.GetShareUI (this, item, shareResult => { flickrButton.Text = "Flickr shared: " + shareResult; }); StartActivity (intent); }, TaskScheduler.FromCurrentSynchronizationContext()); }; Button facebookButton = FindViewById<Button>(Resource.Id.Facebook); facebookButton.Click += (sender, args) => Share (Facebook, facebookButton); Button twitterButton = FindViewById<Button> (Resource.Id.Twitter); twitterButton.Click += (sender, args) => Share (Twitter, twitterButton); }