Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

Null Reference Exception in Xamarin.Forms.Labs.iOS.Services.Media.MediaPicker.SetupController()

$
0
0

I was getting a Null Reference Exception in Xamarin.Forms.Labs.iOS.Services.Media.MediaPicker.SetupController(). The source code for the method follows.

        private static MediaPickerController SetupController (
            MediaPickerDelegate mpDelegate,
            UIImagePickerControllerSourceType sourceType,
            string mediaType,
            MediaStorageOptions options = null)
        {
            var picker = new MediaPickerController (mpDelegate);
            picker.MediaTypes = new[] { mediaType };
            picker.SourceType = sourceType;

            if (sourceType == UIImagePickerControllerSourceType.Camera) {
                picker.CameraDevice = GetUICameraDevice ((options as CameraMediaStorageOptions) .DefaultCamera);

                if (mediaType == TypeImage)
                    picker.CameraCaptureMode = UIImagePickerControllerCameraCaptureMode.Photo;
                else if (mediaType == TypeMovie) {
                    VideoMediaStorageOptions voptions = (VideoMediaStorageOptions)options;
                    picker.CameraCaptureMode = UIImagePickerControllerCameraCaptureMode.Video;
                    picker.VideoQuality = GetQuailty (voptions.Quality);
                    picker.VideoMaximumDuration = voptions.DesiredLength.TotalSeconds;
                }
            }
            return picker;
        }

The problem was that the following line tries to cast options to CameraMediaStorageOptions.

        picker.CameraDevice = GetUICameraDevice ((options as CameraMediaStorageOptions).DefaultCamera);

That is fine when you want to TakePhotoAsync(), but I wanted to TakeVideoAsync(). In this case, options is a VideoMediaStorageOptions which cannot be cast to CameraMediaStorageOptions, so the as returns null. I changed the code as follows, and it now seems to work as desired.

        private static MediaPickerController SetupController (
            MediaPickerDelegate mpDelegate,
            UIImagePickerControllerSourceType sourceType,
            string mediaType,
            MediaStorageOptions options = null)
        {
            var picker = new MediaPickerController (mpDelegate);
            picker.MediaTypes = new[] { mediaType };
            picker.SourceType = sourceType;

            if (sourceType == UIImagePickerControllerSourceType.Camera) {
                if (mediaType == TypeImage) {
                    picker.CameraDevice = GetUICameraDevice ((options as CameraMediaStorageOptions).DefaultCamera);
                    picker.CameraCaptureMode = UIImagePickerControllerCameraCaptureMode.Photo;
                }
                else if (mediaType == TypeMovie) {
                    picker.CameraDevice = GetUICameraDevice ((options as VideoMediaStorageOptions).DefaultCamera);
                    VideoMediaStorageOptions voptions = (VideoMediaStorageOptions)options;
                    picker.CameraCaptureMode = UIImagePickerControllerCameraCaptureMode.Video;
                    picker.VideoQuality = GetQuailty (voptions.Quality);
                    picker.VideoMaximumDuration = voptions.DesiredLength.TotalSeconds;
                }
            }
            return picker;
        }

Now, I did a few searches looking for anyone else who had experienced this problem, but I found no other references to it. I thought surely someone had had this problem, but i failed to find anything directly related to it. So now I am questioning if I have simply been doing something wrong, and if this code change was even necessary. Does anyone have any thoughts on this? Thanks.


Viewing all articles
Browse latest Browse all 58056

Trending Articles