public async Task OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            var mediaPicked = new ObservableCollection <MediaFile>();
            var tasks       = new List <Task>();

            if (requestCode == MultiPickerResultCode)
            {
                if (resultCode == Result.Ok)
                {
                    if (data != null)
                    {
                        ClipData clipData = data.ClipData;
                        if (clipData != null)
                        {
                            for (int i = 0; i < clipData.ItemCount; i++)
                            {
                                ClipData.Item   item = clipData.GetItemAt(i);
                                Android.Net.Uri uri  = item.Uri;

                                tasks.Add(Task.Run(
                                              () =>
                                {
                                    var media = CreateMediaFileFromUri(uri);

                                    if (media != null)
                                    {
                                        lock (Obj)
                                        {
                                            mediaPicked.Add(media);
                                        }
                                        OnMediaPicked?.Invoke(this, media);
                                    }
                                }));
                            }
                        }
                        else
                        {
                            Android.Net.Uri uri   = data.Data;
                            var             media = CreateMediaFileFromUri(uri);
                            if (media != null)
                            {
                                mediaPicked.Add(media);
                                OnMediaPicked?.Invoke(this, media);
                            }
                        }
                    }
                }
                await Task.WhenAll(tasks);

                OnMediaPickedCompleted?.Invoke(this, mediaPicked);
                _mediaPickedTcs?.TrySetResult(mediaPicked);
            }
        }
        public void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            ObservableCollection <MediaModel> oCollection = null;

            if (requestCode == SOURCE_PICK_CODE)
            {
                if (resultCode == Result.Ok)
                {
                    oCollection = new ObservableCollection <MediaModel>();

                    if (data != null)
                    {
                        ClipData oData = data.ClipData;

                        if (oData != null)
                        {
                            for (int i = 0; i < oData.ItemCount; i++)
                            {
                                ClipData.Item   oItem  = oData.GetItemAt(i);
                                Android.Net.Uri oUri   = oItem.Uri;
                                var             oModel = CreateMediaModel(oUri);
                                if (oModel != null)
                                {
                                    oCollection.Add(oModel);
                                    OnMediaPicked?.Invoke(this, oModel);
                                }
                            }
                        }
                        else
                        {
                            Android.Net.Uri oUri   = data.Data;
                            var             oModel = CreateMediaModel(oUri);

                            if (oModel != null)
                            {
                                oCollection.Add(oModel);
                                OnMediaPicked?.Invoke(this, oModel);
                            }
                        }

                        OnMediaPickedCompleted?.Invoke(this, oCollection);
                    }
                }

                _taskCompletionSource?.TrySetResult(oCollection);
            }
        }
Beispiel #3
0
        public void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            ObservableCollection <MediaFile> mediaPicked = null;

            if (requestCode == MultiPickerResultCode)
            {
                if (resultCode == Result.Ok)
                {
                    mediaPicked = new ObservableCollection <MediaFile>();
                    if (data != null)
                    {
                        ClipData clipData = data.ClipData;
                        if (clipData != null)
                        {
                            for (int i = 0; i < clipData.ItemCount; i++)
                            {
                                ClipData.Item   item  = clipData.GetItemAt(i);
                                Android.Net.Uri uri   = item.Uri;
                                var             media = CreateMediaFileFromUri(uri);
                                if (media != null)
                                {
                                    mediaPicked.Add(media);
                                    OnMediaPicked?.Invoke(this, media);
                                }
                            }
                        }
                        else
                        {
                            Android.Net.Uri uri   = data.Data;
                            var             media = CreateMediaFileFromUri(uri);
                            if (media != null)
                            {
                                mediaPicked.Add(media);
                                OnMediaPicked?.Invoke(this, media);
                            }
                        }

                        OnMediaPickedCompleted?.Invoke(this, mediaPicked);
                    }
                }

                mediaPickedTcs?.TrySetResult(mediaPicked);
            }
        }
Beispiel #4
0
        async void FinishedPickingAssets(object sender, MultiAssetEventArgs args)
        {
            IList <MediaFile> results = new List <MediaFile>();
            TaskCompletionSource <IList <MediaFile> > tcs = new TaskCompletionSource <IList <MediaFile> >();

            var options = new Photos.PHImageRequestOptions()
            {
                NetworkAccessAllowed = true
            };

            options.Synchronous  = false;
            options.ResizeMode   = PHImageRequestOptionsResizeMode.Fast;
            options.DeliveryMode = PHImageRequestOptionsDeliveryMode.HighQualityFormat;
            bool completed = false;

            for (var i = 0; i < args.Assets.Length; i++)
            {
                var asset = args.Assets[i];

                string fileName = string.Empty;
                if (UIDevice.CurrentDevice.CheckSystemVersion(9, 0))
                {
                    fileName = PHAssetResource.GetAssetResources(asset).FirstOrDefault().OriginalFilename;
                }

                switch (asset.MediaType)
                {
                case PHAssetMediaType.Video:

                    PHImageManager.DefaultManager.RequestImageForAsset(asset, new SizeF(150.0f, 150.0f),
                                                                       PHImageContentMode.AspectFill, options, async(img, info) =>
                    {
                        var startIndex = fileName.IndexOf(".", StringComparison.CurrentCulture);

                        string path = "";
                        if (startIndex != -1)
                        {
                            path = FileHelper.GetOutputPath(MediaFileType.Image, TemporalDirectoryName, $"{fileName.Substring(0, startIndex)}-THUMBNAIL.JPG");
                        }
                        else
                        {
                            path = FileHelper.GetOutputPath(MediaFileType.Image, TemporalDirectoryName, string.Empty);
                        }

                        if (!File.Exists(path))
                        {
                            img.AsJPEG().Save(path, true);
                        }

                        TaskCompletionSource <string> tvcs = new TaskCompletionSource <string>();

                        var vOptions = new PHVideoRequestOptions();
                        vOptions.NetworkAccessAllowed = true;
                        vOptions.Version      = PHVideoRequestOptionsVersion.Original;
                        vOptions.DeliveryMode = PHVideoRequestOptionsDeliveryMode.FastFormat;


                        PHImageManager.DefaultManager.RequestAvAsset(asset, vOptions, (avAsset, audioMix, vInfo) =>
                        {
                            var vPath = FileHelper.GetOutputPath(MediaFileType.Video, TemporalDirectoryName, fileName);

                            if (!File.Exists(vPath))
                            {
                                AVAssetExportSession exportSession = new AVAssetExportSession(avAsset, AVAssetExportSession.PresetHighestQuality);

                                exportSession.OutputUrl      = NSUrl.FromFilename(vPath);
                                exportSession.OutputFileType = AVFileType.QuickTimeMovie;


                                exportSession.ExportAsynchronously(() =>
                                {
                                    Console.WriteLine(exportSession.Status);

                                    tvcs.TrySetResult(vPath);
                                    //exportSession.Dispose();
                                });
                            }
                        });

                        var videoUrl = await tvcs.Task;
                        var meFile   = new MediaFile()
                        {
                            Type        = MediaFileType.Video,
                            Path        = videoUrl,
                            PreviewPath = path
                        };
                        results.Add(meFile);
                        OnMediaPicked?.Invoke(this, meFile);

                        if (args.Assets.Length == results.Count && !completed)
                        {
                            completed = true;
                            tcs.TrySetResult(results);
                        }
                    });



                    break;

                default:

                    Photos.PHImageManager.DefaultManager.RequestImageData(asset, options, (data, dataUti, orientation, info) =>
                    {
                        string path = FileHelper.GetOutputPath(MediaFileType.Image, TemporalDirectoryName, fileName);

                        if (!File.Exists(path))
                        {
                            Debug.WriteLine(dataUti);
                            var imageData = data;
                            //var image = UIImage.LoadFromData(imageData);

                            //if (imageScale < 1.0f)
                            //{
                            //    //begin resizing image
                            //    image = image.ResizeImageWithAspectRatio((float)imageScale);
                            //}

                            //if (imageQuality < 100)
                            //{
                            //    imageData = image.AsJPEG(Math.Min(imageQuality,100));
                            //}

                            imageData?.Save(path, true);
                        }

                        var meFile = new MediaFile()
                        {
                            Type        = MediaFileType.Image,
                            Path        = path,
                            PreviewPath = path
                        };

                        results.Add(meFile);
                        OnMediaPicked?.Invoke(this, meFile);
                        if (args.Assets.Length == results.Count && !completed)
                        {
                            completed = true;
                            tcs.TrySetResult(results);
                        }
                    });

                    break;
                }
            }


            mediaPickTcs?.TrySetResult(await tcs.Task);
        }