Exemple #1
0
        public async Task <FileResult> PickAsync(MediaPickerOptions options, bool photo)
        {
            var picker = new FileOpenPicker();

            var hwnd = WindowStateManager.Default.GetActiveWindowHandle(true);

            WinRT.Interop.InitializeWithWindow.Initialize(picker, hwnd);

            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));
        }
Exemple #2
0
        public async Task <FileResult> PickAsync(MediaPickerOptions options, bool photo)
        {
            var intent = new Intent(Intent.ActionGetContent);

            intent.SetType(photo ? FileMimeTypes.ImageAll : FileMimeTypes.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 = FileSystemUtils.EnsurePhysicalPath(intent.Data);
                }

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

                return(new FileResult(path));
            }
            catch (OperationCanceledException)
            {
                return(null);
            }
        }
Exemple #3
0
        public async Task <FileResult> MediaAsync(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);
        }
Exemple #4
0
        public Task <FileResult> CaptureVideoAsync(MediaPickerOptions options)
        {
            if (!IsCaptureSupported)
            {
                throw new FeatureNotSupportedException();
            }

            return(PhotoAsync(options, false, false));
        }
Exemple #5
0
        public async Task <FileResult> CaptureAsync(MediaPickerOptions options, bool photo)
        {
            if (!IsCaptureSupported)
            {
                throw new FeatureNotSupportedException();
            }

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

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

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

            if (!PlatformUtils.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 = ActivityStateManager.Default.GetCurrentActivity(true);

                // Create the temporary file
                var ext = photo
                                        ? FileExtensions.Jpg
                                        : FileExtensions.Mp4;
                var fileName = Guid.NewGuid().ToString("N") + ext;
                var tmpFile  = FileSystemUtils.GetTemporaryFile(Application.Context.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, PlatformUtils.requestCodeMediaCapture, OnCreate);

                // Return the file that we just captured
                return(new FileResult(tmpFile.AbsolutePath));
            }
            catch (OperationCanceledException)
            {
                return(null);
            }
        }
Exemple #6
0
        public 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);
        }
Exemple #7
0
        public 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 && !pickExisting)
            {
                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 && !OperatingSystem.IsIOSVersionAtLeast(11, 0))
            {
                await Permissions.EnsureGrantedAsync <Permissions.Photos>();
            }

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

            var vc = WindowStateManager.Default.GetCurrentUIViewController(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.Current.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 = async info =>
                {
                    GetFileResult(info, tcs);
                    await vc.DismissViewControllerAsync(true);
                }
            };

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

            await vc.PresentViewControllerAsync(picker, true);

            var result = await tcs.Task;

            picker?.Dispose();
            picker = null;

            return(result);
        }
Exemple #8
0
 public Task <FileResult> PickVideoAsync(MediaPickerOptions options)
 => PhotoAsync(options, false, true);
Exemple #9
0
 public Task <FileResult> CaptureVideoAsync(MediaPickerOptions options)
 => CaptureAsync(options, false);
Exemple #10
0
 public Task <FileResult> CapturePhotoAsync(MediaPickerOptions options)
 => CaptureAsync(options, true);
Exemple #11
0
 public Task <FileResult> PickPhotoAsync(MediaPickerOptions options)
 => PickAsync(options, true);
Exemple #12
0
 public Task <FileResult> CaptureVideoAsync(MediaPickerOptions options) =>
 throw new NotImplementedInReferenceAssemblyException();
Exemple #13
0
 public Task <FileResult> PlatformCaptureVideoAsync(MediaPickerOptions options)
 => PlatformPickVideoAsync(options);
Exemple #14
0
 public async Task <FileResult> PlatformPickVideoAsync(MediaPickerOptions options)
 => new FileResult(await FilePicker.PickAsync(new PickOptions
 {
     FileTypes = FilePickerFileType.Videos
 }));
Exemple #15
0
 public async Task <FileResult> PickVideoAsync(MediaPickerOptions options)
 => await FilePicker.PickAsync(new PickOptions
 {
     PickerTitle = options?.Title,
     FileTypes   = FilePickerFileType.Videos
 });