static async Task <FileResult> PickAsync(MediaPickerOptions options, bool photo)
        {
            var picker = new FileOpenPicker();

            var defaultTypes = photo ? FilePickerFileType.Images.Value : FilePickerFileType.Videos.Value;

            // set picker properties
            foreach (var filter in defaultTypes.Select(t => t.TrimStart('*')))
            {
                picker.FileTypeFilter.Add(filter);
            }
            picker.SuggestedStartLocation = photo ? PickerLocationId.PicturesLibrary : PickerLocationId.VideosLibrary;
            picker.ViewMode = PickerViewMode.Thumbnail;

            // show the picker
            var result = await picker.PickSingleFileAsync();

            // cancelled
            if (result == null)
            {
                return(null);
            }

            // picked
            return(new FileResult(result));
        }
Esempio n. 2
0
        static async Task <FileResult> PlatformPickAsync(MediaPickerOptions options, bool photo)
        {
            // We only need the permission when accessing the file, but it's more natural
            // to ask the user first, then show the picker.
            await Permissions.EnsureGrantedAsync <Permissions.StorageRead>();

            var intent = new Intent(Intent.ActionGetContent);

            intent.SetType(photo ? FileSystem.MimeTypes.ImageAll : FileSystem.MimeTypes.VideoAll);

            var pickerIntent = Intent.CreateChooser(intent, options?.Title);

            try
            {
                string path = null;
                void OnResult(Intent intent)
                {
                    // The uri returned is only temporary and only lives as long as the Activity that requested it,
                    // so this means that it will always be cleaned up by the time we need it because we are using
                    // an intermediate activity.

                    path = FileSystem.EnsurePhysicalPath(intent.Data);
                }

                await IntermediateActivity.StartAsync(pickerIntent, Platform.requestCodeMediaPicker, onResult : OnResult);

                return(new FileResult(path));
            }
            catch (OperationCanceledException)
            {
                return(null);
            }
        }
Esempio n. 3
0
        static async Task <FileResult> PlatformMediaAsync(MediaPickerOptions options, bool photo)
        {
            Permissions.EnsureDeclared <Permissions.LaunchApp>();

            await Permissions.EnsureGrantedAsync <Permissions.StorageRead>();

            var tcs = new TaskCompletionSource <FileResult>();

            var appControl = new AppControl();

            appControl.Operation  = photo ? AppControlOperations.ImageCapture : AppControlOperations.VideoCapture;
            appControl.LaunchMode = AppControlLaunchMode.Group;

            var appId = AppControl.GetMatchedApplicationIds(appControl)?.FirstOrDefault();

            if (!string.IsNullOrEmpty(appId))
            {
                appControl.ApplicationId = appId;
            }

            AppControl.SendLaunchRequest(appControl, (request, reply, result) =>
            {
                if (result == AppControlReplyResult.Succeeded && reply.ExtraData.Count() > 0)
                {
                    var file = reply.ExtraData.Get <IEnumerable <string> >(AppControlData.Selected)?.FirstOrDefault();
                    tcs.TrySetResult(new FileResult(file));
                }
                else
                {
                    tcs.TrySetCanceled();
                }
            });

            return(await tcs.Task);
        }
Esempio n. 4
0
        public static Task <FileResult> CaptureVideoAsync(MediaPickerOptions options = null)
        {
            if (!IsCaptureSupported)
            {
                throw new FeatureNotSupportedException();
            }

            return(PlatformCaptureVideoAsync(options));
        }
Esempio n. 5
0
        /// <include file="../../docs/Microsoft.Maui.Essentials/MediaPicker.xml" path="//Member[@MemberName='CapturePhotoAsync']/Docs" />
        public static Task <FileResult> CapturePhotoAsync(MediaPickerOptions options = null)
        {
            if (!IsCaptureSupported)
            {
                throw new FeatureNotSupportedException();
            }

            return(Current.CapturePhotoAsync(options));
        }
Esempio n. 6
0
        static async Task <FileResult> PlatformCaptureAsync(MediaPickerOptions options, bool photo)
        {
            await Permissions.EnsureGrantedAsync <Permissions.Camera>();

            await Permissions.EnsureGrantedAsync <Permissions.StorageWrite>();

            var capturePhotoIntent = new Intent(photo ? MediaStore.ActionImageCapture : MediaStore.ActionVideoCapture);

            if (!Platform.IsIntentSupported(capturePhotoIntent))
            {
                throw new FeatureNotSupportedException($"Either there was no camera on the device or '{capturePhotoIntent.Action}' was not added to the <queries> element in the app's manifest file. See more: https://developer.android.com/about/versions/11/privacy/package-visibility");
            }

            capturePhotoIntent.AddFlags(ActivityFlags.GrantReadUriPermission);
            capturePhotoIntent.AddFlags(ActivityFlags.GrantWriteUriPermission);

            try
            {
                var activity = Platform.GetCurrentActivity(true);

                // Create the temporary file
                var ext = photo
                                        ? FileSystem.Extensions.Jpg
                                        : FileSystem.Extensions.Mp4;
                var fileName = Guid.NewGuid().ToString("N") + ext;
                var tmpFile  = FileSystem.GetEssentialsTemporaryFile(Platform.AppContext.CacheDir, fileName);

                // Set up the content:// uri
                AndroidUri outputUri = null;
                void OnCreate(Intent intent)
                {
                    // Android requires that using a file provider to get a content:// uri for a file to be called
                    // from within the context of the actual activity which may share that uri with another intent
                    // it launches.

                    outputUri ??= FileProvider.GetUriForFile(tmpFile);

                    intent.PutExtra(MediaStore.ExtraOutput, outputUri);
                }

                // Start the capture process
                await IntermediateActivity.StartAsync(capturePhotoIntent, Platform.requestCodeMediaCapture, OnCreate);

                // Return the file that we just captured
                return(new FileResult(tmpFile.AbsolutePath));
            }
            catch (OperationCanceledException)
            {
                return(null);
            }
        }
        static async Task <FileResult> CaptureAsync(MediaPickerOptions options, bool photo)
        {
            var captureUi = new CameraCaptureUI();

            if (photo)
            {
                captureUi.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;
            }
            else
            {
                captureUi.VideoSettings.Format = CameraCaptureUIVideoFormat.Mp4;
            }

            var file = await captureUi.CaptureFileAsync(photo?CameraCaptureUIMode.Photo : CameraCaptureUIMode.Video);

            if (file != null)
            {
                return(new FileResult(file));
            }

            return(null);
        }
Esempio n. 8
0
 static async Task <FileResult> PlatformPickVideoAsync(MediaPickerOptions options)
 => await FilePicker.PickAsync(new PickOptions
 {
     PickerTitle = options?.Title,
     FileTypes   = FilePickerFileType.Videos
 });
 static Task <FileResult> PlatformCapturePhotoAsync(MediaPickerOptions options)
 => CaptureAsync(options, true);
Esempio n. 10
0
 static Task <FileResult> PlatformCaptureVideoAsync(MediaPickerOptions options)
 => CaptureAsync(options, false);
Esempio n. 11
0
 static Task <FileResult> PlatformPickVideoAsync(MediaPickerOptions options)
 => PickAsync(options, false);
Esempio n. 12
0
 public static Task <FileResult> PickVideoAsync(MediaPickerOptions options = null) =>
 PlatformPickVideoAsync(options);
Esempio n. 13
0
 static Task <FileResult> PlatformPickPhotoAsync(MediaPickerOptions options)
 => PickAsync(options, true);
Esempio n. 14
0
        static async Task <FileResult> PhotoAsync(MediaPickerOptions options, bool photo, bool pickExisting)
        {
            var sourceType = pickExisting ? UIImagePickerControllerSourceType.PhotoLibrary : UIImagePickerControllerSourceType.Camera;
            var mediaType  = photo ? UTType.Image : UTType.Movie;

            if (!UIImagePickerController.IsSourceTypeAvailable(sourceType))
            {
                throw new FeatureNotSupportedException();
            }
            if (!UIImagePickerController.AvailableMediaTypes(sourceType).Contains(mediaType))
            {
                throw new FeatureNotSupportedException();
            }

            if (!photo)
            {
                await Permissions.EnsureGrantedAsync <Permissions.Microphone>();
            }

            // Check if picking existing or not and ensure permission accordingly as they can be set independently from each other
            if (pickExisting && !Platform.HasOSVersion(11, 0))
            {
                await Permissions.EnsureGrantedAsync <Permissions.Photos>();
            }

            if (!pickExisting)
            {
                await Permissions.EnsureGrantedAsync <Permissions.Camera>();
            }

            var vc = Platform.GetCurrentViewController(true);

            picker               = new UIImagePickerController();
            picker.SourceType    = sourceType;
            picker.MediaTypes    = new string[] { mediaType };
            picker.AllowsEditing = false;
            if (!photo && !pickExisting)
            {
                picker.CameraCaptureMode = UIImagePickerControllerCameraCaptureMode.Video;
            }

            if (!string.IsNullOrWhiteSpace(options?.Title))
            {
                picker.Title = options.Title;
            }

            if (DeviceInfo.Idiom == DeviceIdiom.Tablet && picker.PopoverPresentationController != null && vc.View != null)
            {
                picker.PopoverPresentationController.SourceRect = vc.View.Bounds;
            }

            var tcs = new TaskCompletionSource <FileResult>(picker);

            picker.Delegate = new PhotoPickerDelegate
            {
                CompletedHandler = info => GetFileResult(info, tcs)
            };

            if (picker.PresentationController != null)
            {
                picker.PresentationController.Delegate = new PhotoPickerPresentationControllerDelegate
                {
                    CompletedHandler = info => GetFileResult(info, tcs)
                };
            }

            await vc.PresentViewControllerAsync(picker, true);

            var result = await tcs.Task;

            await vc.DismissViewControllerAsync(true);

            picker?.Dispose();
            picker = null;

            return(result);
        }
Esempio n. 15
0
 static Task <FileResult> PlatformCaptureVideoAsync(MediaPickerOptions options) =>
 throw new NotImplementedInReferenceAssemblyException();
Esempio n. 16
0
 /// <include file="../../docs/Microsoft.Maui.Essentials/MediaPicker.xml" path="//Member[@MemberName='PickVideoAsync']/Docs" />
 public static Task <FileResult> PickVideoAsync(MediaPickerOptions options = null) =>
 Current.PickVideoAsync(options);
Esempio n. 17
0
 static async Task <FileResult> PlatformPickVideoAsync(MediaPickerOptions options)
 => new FileResult(await FilePicker.PickAsync(new PickOptions
 {
     FileTypes = FilePickerFileType.Videos
 }));