public void GetImageAsync(Action <MediaFile> imageData, bool fromCamera = true) { OnImageData = imageData; var context = Forms.Context as MainActivity; var picker = new MediaPicker(context); if (!picker.IsCameraAvailable) { Console.WriteLine("No camera!"); } else { Intent intent = null; if (fromCamera) { intent = picker.GetTakePhotoUI(new StoreCameraMediaOptions { Name = string.Format("vDoers_{0}.jpg", DateTime.Now.Ticks), Directory = "vDoersCamera" }); } else { intent = picker.GetPickPhotoUI(); } context.OnActvitiResultCallback -= OnActvitiResultCallback; context.OnActvitiResultCallback += OnActvitiResultCallback; context.StartActivityForResult(intent, 1); } }
private void ButtonSendAttachmentEmail_Click(object sender, EventArgs eventArgs) { var picker = new MediaPicker(Application.Context); var intent = picker.GetPickPhotoUI(); StartActivityForResult(intent, SelectPhoto); }
private void chooseFromGallery() { var picker = new MediaPicker(this); var intent = picker.GetPickPhotoUI(); StartActivityForResult(intent, 99); }
public void TakePictureFromFile() { var applicationContext = Forms.Context as Activity; var picker = new MediaPicker(applicationContext); var intent = picker.GetPickPhotoUI(); applicationContext.StartActivityForResult(intent, 1); }
public void SelecionarFoto() { var context = Forms.Context as Activity; var captura = new MediaPicker(context); var intent = captura.GetPickPhotoUI(); context.StartActivityForResult(intent, 1); }
public void SelectPic() { var activity = Forms.Context as Activity; var picker = new MediaPicker(activity); var intent = picker.GetPickPhotoUI(); activity.StartActivityForResult(intent, 1); }
public async Task <Stream> PickPhoto() { var picker = new MediaPicker(Application.Context); var intent = picker.GetPickPhotoUI(); intent.SetFlags(ActivityFlags.NewTask); Application.Context.StartActivity(intent); return(null); }
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); var intentPicker = picker.GetPickPhotoUI(); StartActivityForResult(intentPicker, 1); }; Button facebookButton = FindViewById <Button>(Resource.Id.Facebook); facebookButton.Click += (sender, args) => { try { Share(Facebook, facebookButton); } catch (Exception ex) { ShowMessage("Facebook: " + ex.Message); } }; Button twitterButton = FindViewById <Button> (Resource.Id.Twitter); twitterButton.Click += (sender, args) => { try { Share(Twitter, twitterButton); } catch (Exception ex) { ShowMessage("Twitter: " + ex.Message); } }; }
private void ChoosePhoto_Click(object sender, EventArgs e) { try { var picker = new MediaPicker(this); if (!picker.PhotosSupported) { ShowUnsupported(); return; } var intent = picker.GetPickPhotoUI(); this.StartActivityForResult(intent, 2); } catch (Exception exception) { GaService.TrackAppException(this.Class, "ChoosePhoto_Click", exception, false); } }
protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); var picker = new MediaPicker(this); if (!picker.IsCameraAvailable) { Console.WriteLine("No camera!"); } else { //var intent = picker.GetTakePhotoUI(new StoreCameraMediaOptions //{ // Name = "test.jpg", // Directory = "MediaPickerSample" //}); var intent = picker.GetPickPhotoUI(); taskId = Intent.GetIntExtra("taskId", 1); StartActivityForResult(intent, 1); } }
async void SelectFromGallery() { var picker = new MediaPicker(); MediaPickerController controller = picker.GetPickPhotoUI(); // On iPad, you'll use UIPopoverController to present the controller PresentViewController(controller, true, null); //var result = await controller.GetResultAsync(); //controller.DismissViewController(true, null); //MediaFile file = result; //_page.ImagePath = file.Path; await controller.GetResultAsync().ContinueWith(t => { // We need to dismiss the controller ourselves controller.DismissViewController(true, () => { // User canceled or something went wrong if (t.IsCanceled || t.IsFaulted) { return; } MediaFile media = t.Result; MemoryStream mstr = new MemoryStream(); media.GetStream().CopyTo(mstr); if (_basePage != null) { _basePage.ImagePath = media.Path; _basePage.bytes = ResizeTheImage(mstr.ToArray(), 800, 800); _basePage.Source = ImageSource.FromStream(() => new MemoryStream(_basePage.bytes)); } }); // Make sure we use the UI thread to show our photo. }, TaskScheduler.FromCurrentSynchronizationContext()); }
public void GetImageAsync(Action <MediaFile> imageData, bool fromCamera = true) { var picker = new MediaPicker(); MediaPickerController controller = null; if (fromCamera) { controller = picker.GetTakePhotoUI(new StoreCameraMediaOptions { Name = string.Format("vDoers_{0}.jpg", DateTime.Now.Ticks), Directory = "vDoersCamera" }); } else { controller = picker.GetPickPhotoUI(); } var window = UIApplication.SharedApplication.KeyWindow; var vc = window.RootViewController; if (vc != null) { // vc = vc.PresentedViewController; vc.PresentViewController(controller, true, null); } controller.GetResultAsync().ContinueWith(t => { // Dismiss the UI yourself controller.DismissViewController(true, () => { if (imageData != null) { imageData(t.Result); } }); }, TaskScheduler.FromCurrentSynchronizationContext()); }
public void PickImage(String defaultImageName) { if (_mediaPicker == null) { _mediaPicker = new MediaPicker(this); } String negative = _mediaPicker.IsCameraAvailable ? "Camera" : "Cancel"; int result = GetUserResponse(this, "Use Image from", "Default", "Photo Library", negative); if (result > 0) { OnImagePicked(this, new Mat(Assets, defaultImageName)); } else if (result == 0) { Intent intent = _mediaPicker.GetPickPhotoUI(); StartActivityForResult(intent, _pickImageRequestCode); } else if (_mediaPicker.IsCameraAvailable) { Intent intent = _mediaPicker.GetTakePhotoUI(new StoreCameraMediaOptions()); StartActivityForResult(intent, _pickImageRequestCode); } }
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 async void LoadImage(String imageName) { #if NETFX_CORE Mat m = CvInvoke.Imread(imageName, LoadImageType.AnyColor); InvokeOnImageLoaded(m); #else if (_mediaPicker == null) { #if __ANDROID__ _mediaPicker = new MediaPicker(Forms.Context); #else _mediaPicker = new MediaPicker(); #endif } var action = await(_mediaPicker.IsCameraAvailable? DisplayActionSheet("Use Image from", "Cancel", null, "Default", "Photo Library", "Camera") : DisplayActionSheet("Use Image from", "Cancel", null, "Default", "Photo Library")); if (action.Equals("Default")) { #if __ANDROID__ InvokeOnImageLoaded(new Mat(Forms.Context.Assets, imageName)); #else Mat m = CvInvoke.Imread(imageName, LoadImageType.AnyColor); InvokeOnImageLoaded(m); #endif } else if (action.Equals("Photo Library")) { #if __ANDROID__ Android.Content.Intent intent = _mediaPicker.GetPickPhotoUI(); Android.App.Activity activity = Forms.Context as Android.App.Activity; activity.StartActivityForResult(intent, PickImageRequestCode); //once the image was picked, the MainActivity.OnActivityResult function will handle the remaining work flow #else var file = await _mediaPicker.PickPhotoAsync(); using (Stream s = file.GetStream()) using (MemoryStream ms = new MemoryStream()) { s.CopyTo(ms); byte[] data = ms.ToArray(); Mat m = new Mat(); CvInvoke.Imdecode(data, LoadImageType.Color, m); InvokeOnImageLoaded(m); } #endif } else if (action.Equals("Camera")) { #if __ANDROID__ Android.Content.Intent intent = _mediaPicker.GetTakePhotoUI(new StoreCameraMediaOptions()); Android.App.Activity activity = Forms.Context as Android.App.Activity; activity.StartActivityForResult(intent, PickImageRequestCode); //once the image was picked, the MainActivity.OnActivityResult function will handle the remaining work flow #else var file = await _mediaPicker.TakePhotoAsync(new StoreCameraMediaOptions()); using (Stream s = file.GetStream()) using (MemoryStream ms = new MemoryStream()) { s.CopyTo(ms); byte[] data = ms.ToArray(); Mat m = new Mat(); CvInvoke.Imdecode(data, LoadImageType.Color, m); InvokeOnImageLoaded(m); } #endif } #endif }
public virtual async void LoadImages(String[] imageNames, String[] labels = null) { #if NETFX_CORE Mat[] mats = new Mat[imageNames.Length]; for (int i = 0; i < mats.Length; i++) { mats[i] = CvInvoke.Imread(imageNames[i], ImreadModes.Color); } InvokeOnImagesLoaded(mats); #else if (_mediaPicker == null) { #if __ANDROID__ _mediaPicker = new MediaPicker(Forms.Context); #else _mediaPicker = new MediaPicker(); #endif } Mat[] mats = new Mat[imageNames.Length]; for (int i = 0; i < mats.Length; i++) { String pickImgString = "Use Image from"; if (labels != null && labels.Length > i) { pickImgString = labels[i]; } var action = await(_mediaPicker.IsCameraAvailable? DisplayActionSheet(pickImgString, "Cancel", null, "Default", "Photo Library", "Camera") : DisplayActionSheet(pickImgString, "Cancel", null, "Default", "Photo Library")); if (action.Equals("Default")) { #if __ANDROID__ mats[i] = new Mat(Forms.Context.Assets, imageNames[i]); #else mats[i] = CvInvoke.Imread(imageNames[i], ImreadModes.AnyColor); #endif } else if (action.Equals("Photo Library")) { #if __ANDROID__ Android.Content.Intent intent = _mediaPicker.GetPickPhotoUI(); Android.App.Activity activity = Forms.Context as Android.App.Activity; activity.StartActivityForResult(intent, PickImageRequestCode); //once the image was picked, the MainActivity.OnActivityResult function will handle the remaining work flow Task t = new Task(() => { _waitHandle.WaitOne(); }); t.Start(); await t; if (MatHandle == null) //Cancelled { return; } mats[i] = MatHandle; #else var file = await _mediaPicker.PickPhotoAsync(); using (Stream s = file.GetStream()) using (MemoryStream ms = new MemoryStream()) { s.CopyTo(ms); byte[] data = ms.ToArray(); Mat m = new Mat(); CvInvoke.Imdecode(data, ImreadModes.Color, m); mats[i] = m; } #endif } else if (action.Equals("Camera")) { #if __ANDROID__ Android.Content.Intent intent = _mediaPicker.GetTakePhotoUI(new StoreCameraMediaOptions()); Android.App.Activity activity = Forms.Context as Android.App.Activity; activity.StartActivityForResult(intent, PickImageRequestCode); //once the image was picked, the MainActivity.OnActivityResult function will handle the remaining work flow Task t = new Task(() => { _waitHandle.WaitOne(); }); t.Start(); await t; if (MatHandle == null) //Cancelled { return; } mats[i] = MatHandle; #else var file = await _mediaPicker.TakePhotoAsync(new StoreCameraMediaOptions()); using (Stream s = file.GetStream()) using (MemoryStream ms = new MemoryStream()) { s.CopyTo(ms); byte[] data = ms.ToArray(); Mat m = new Mat(); CvInvoke.Imdecode(data, ImreadModes.Color, m); mats[i] = m; } #endif } } InvokeOnImagesLoaded(mats); #endif }
protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); SetContentView(Resource.Layout.Main); Button videoButton = FindViewById <Button> (Resource.Id.takeVideoButton); videoButton.Click += delegate { // 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; } // The GetTakeVideoUI method returns an Intent to start // the native camera app to record a video. Intent intent = picker.GetTakeVideoUI(new StoreVideoOptions { Name = "MyVideo", Directory = "MyVideos", DesiredLength = TimeSpan.FromSeconds(10) }); StartActivityForResult(intent, 1); }; Button photoButton = FindViewById <Button> (Resource.Id.takePhotoButton); photoButton.Click += delegate { var picker = new MediaPicker(this); if (!picker.IsCameraAvailable || !picker.PhotosSupported) { ShowUnsupported(); return; } Intent intent = picker.GetTakePhotoUI(new StoreCameraMediaOptions { Name = "test.jpg", Directory = "MediaPickerSample" }); StartActivityForResult(intent, 2); }; Button pickVideoButton = FindViewById <Button> (Resource.Id.pickVideoButton); pickVideoButton.Click += delegate { var picker = new MediaPicker(this); if (!picker.VideosSupported) { ShowUnsupported(); return; } // The GetPickVideoUI() method returns an Intent to start // the native gallery app to select a video. Intent intent = picker.GetPickVideoUI(); StartActivityForResult(intent, 1); }; Button pickPhotoButton = FindViewById <Button> (Resource.Id.pickPhotoButton); pickPhotoButton.Click += delegate { var picker = new MediaPicker(this); if (!picker.PhotosSupported) { ShowUnsupported(); return; } Intent intent = picker.GetPickPhotoUI(new StoreCameraMediaOptions { Name = "selected.jpg", Directory = "MediaPickerSample" }); StartActivityForResult(intent, 2); }; }
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); }