コード例 #1
0
        private void ExtractVideoPickedFromGallery(Intent data)
        {
            try {
                Android.Net.Uri selectedUri = data.Data;

                string mimeType = "video";

                PickedMediaFromGallery result = new PickedMediaFromGallery();

                if (mimeType.StartsWith("video"))
                {
                    string filePath = MediaUtils.GetFileFullPathAlternativeVideo(data.Data, this);

                    Java.IO.File videoFile = new Java.IO.File(filePath);

                    Java.IO.FileInputStream videoFileInputStream = new Java.IO.FileInputStream(videoFile);
                    byte[] videoFileBytes = new byte[videoFile.Length()];
                    videoFileInputStream.Read(videoFileBytes);
                    videoFileInputStream.Close();
                    videoFileInputStream.Dispose();

                    System.IO.Stream thumbnailStream = DependencyService.Get <IPickMediaDependencyService>().GetThumbnail(filePath);
                    byte[]           thumbnailBytes;

                    thumbnailStream.Position = 0;

                    using (System.IO.BinaryReader reader = new System.IO.BinaryReader(thumbnailStream)) {
                        thumbnailBytes = reader.ReadBytes((int)thumbnailStream.Length);
                    }

                    result = new PickedMediaFromGallery()
                    {
                        Completion          = true,
                        DataBase64          = Android.Util.Base64.EncodeToString(videoFileBytes, Android.Util.Base64Flags.Default),
                        DataThumbnailBase64 = Convert.ToBase64String(thumbnailBytes),
                        FilePath            = filePath,
                        MimeType            = ProfileMediaService.VIDEO_MEDIA_TYPE
                    };
                }
                else
                {
                    Debugger.Break();
                    throw new InvalidOperationException();
                }

                PickVideoTaskCompletion.SetResult(result);
            }
            catch (Exception exc) {
                Debugger.Break();

                PickVideoTaskCompletion.SetResult(new PickedMediaFromGallery()
                {
                    Exception = exc, ErrorMessage = _EXTRACTION_MEDIA_MEDIA_PICK_ERROR
                });
            }
        }
コード例 #2
0
        public Task <IPickedMediaFromGallery> PickVideoAsync()
        {
            UIViewController topController = UIApplication.SharedApplication.KeyWindow.RootViewController;

            while (topController.PresentedViewController != null)
            {
                topController = topController.PresentedViewController;
            }

            _pickVideoTaskCompletion = new TaskCompletionSource <IPickedMediaFromGallery>();

            UIImagePickerController imagePicker = new UIImagePickerController();

            imagePicker.SourceType = UIImagePickerControllerSourceType.PhotoLibrary;
            imagePicker.MediaTypes = new string[] { UTType.Movie };

            imagePicker.FinishedPickingMedia += (sender, args) => {
                IPickedMediaFromGallery result = null;

                try {
                    string mediaTypeInfo = args.Info[UIImagePickerController.MediaType].ToString();

                    if (mediaTypeInfo == _MOVIE_MEDIA_TYPE)
                    {
                        result = HandlePickedMovie(args);
                    }
                    else
                    {
                        result = new PickedMediaFromGallery();
                    }
                }
                catch (Exception exc) {
                    result = new PickedMediaFromGallery()
                    {
                        Exception = exc
                    };
                }

                topController.DismissModalViewController(true);

                _pickVideoTaskCompletion.SetResult(result);
            };

            imagePicker.Canceled += (sender, args) => {
                topController.DismissModalViewController(true);

                _pickVideoTaskCompletion.SetResult(new PickedMediaFromGallery()
                {
                    Completion = true
                });
            };

            topController.PresentModalViewController(imagePicker, true);

            return(_pickVideoTaskCompletion.Task);
        }
コード例 #3
0
        private void ExtractMediaPickedFromGallery(Intent data)
        {
            try {
                Android.Net.Uri selectedUri = data.Data;
                String[]        columns     = { MediaStore.Images.Media.InterfaceConsts.Data,
                                                MediaStore.Images.Media.InterfaceConsts.MimeType };

                ICursor cursor = ContentResolver.Query(selectedUri, columns, null, null, null);
                cursor.MoveToFirst();

                int pathColumnIndex     = cursor.GetColumnIndex(columns[0]);
                int mimeTypeColumnIndex = cursor.GetColumnIndex(columns[1]);

                string mimeType = cursor.GetString(mimeTypeColumnIndex);
                cursor.Close();

                PickedMediaFromGallery result = null;

                if (mimeType.StartsWith("image"))
                {
                    Android.Graphics.Bitmap bitmap = MediaUtils.GetScaledBitmap(
                        data.Data,
                        MediaUtils.IMAGE_WIDTH_RESTRICTION,
                        MediaUtils.IMAGE_HEIGHT_RESTRICTION,
                        this);

                    if (bitmap == null)
                    {
                        bitmap = MediaStore.Images.Media.GetBitmap(ContentResolver, data.Data);
                    }

                    byte[] bytes;

                    using (MemoryStream memoryStream = new MemoryStream()) {
                        bitmap.Compress(Android.Graphics.Bitmap.CompressFormat.Jpeg, 100, memoryStream);
                        bytes = memoryStream.GetBuffer();
                    }

                    result = new PickedMediaFromGallery()
                    {
                        DataBase64          = Android.Util.Base64.EncodeToString(bytes, Android.Util.Base64Flags.Default),
                        Completion          = true,
                        DataThumbnailBase64 = Android.Util.Base64.EncodeToString(bytes, Android.Util.Base64Flags.Default),
                        MimeType            = ProfileMediaService.IMAGE_MEDIA_TYPE
                    };
                }
                else if (mimeType.StartsWith("video"))
                {
                    string filePath = MediaUtils.GetFileFullPathAlternativeVideo(data.Data, this);

                    Java.IO.File videoFile = new Java.IO.File(filePath);

                    Java.IO.FileInputStream videoFileInputStream = new Java.IO.FileInputStream(videoFile);
                    byte[] videoFileBytes = new byte[videoFile.Length()];
                    videoFileInputStream.Read(videoFileBytes);
                    videoFileInputStream.Close();
                    videoFileInputStream.Dispose();

                    System.IO.Stream thumbnailStream = DependencyService.Get <IPickMediaDependencyService>().GetThumbnail(filePath);
                    byte[]           thumbnailBytes;

                    thumbnailStream.Position = 0;

                    using (System.IO.BinaryReader reader = new System.IO.BinaryReader(thumbnailStream)) {
                        thumbnailBytes = reader.ReadBytes((int)thumbnailStream.Length);
                    }

                    result = new PickedMediaFromGallery()
                    {
                        Completion          = true,
                        DataBase64          = Android.Util.Base64.EncodeToString(videoFileBytes, Android.Util.Base64Flags.Default),
                        DataThumbnailBase64 = Convert.ToBase64String(thumbnailBytes),
                        FilePath            = filePath,
                        MimeType            = ProfileMediaService.VIDEO_MEDIA_TYPE
                    };
                }
                else
                {
                    Debugger.Break();
                    throw new InvalidOperationException();
                }

                PickMediaTaskCompletion.SetResult(result);
            }
            catch (Exception exc) {
                Debugger.Break();

                PickMediaTaskCompletion.SetResult(new PickedMediaFromGallery()
                {
                    Exception = exc, ErrorMessage = _EXTRACTION_MEDIA_MEDIA_PICK_ERROR
                });
            }
        }