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

FilePath from MediaPicker randomly fails

$
0
0

Hi,

I'm using the failry standard MediaPicker to select Media from iOS:

            var mediaPicker = new MediaPicker();
                    await mediaPicker.PickPhotoAsync().ContinueWith(t =>
                    {
                        MediaFile file = t.Result;

                        if (file != null)
                        {
                            _viewModel.ImagePath = file.Path;
                            chosenImg.Source = ImageSource.FromFile(file.Path);
                        }

                    }, TaskScheduler.FromCurrentSynchronizationContext());

The strange thing here is that chosenImg, which is just an Image Control, will ALWAYS display the correct image so I know the image selected is there and the path must be right in some form. However, I then use the Dependency Service to take the image and resize it before the user uploads.

This is where the problem lies, when the platform specific code attempts to resize the image, it sometimes finds the file and sometimes doesn't. At first I thought it was a simulator bug, but the same behaviour occurs on my device as well.

The code is really simple for resizing, it just takes pathToFile (which I know must be correct in some form because the Image Control displays it), and a targetPath and a max length/width:

public async Task ResizeImage(string sourceFile, string targetFile, float maxWidth, float maxHeight)
        {
            try
            {
                if (File.Exists(sourceFile) && !File.Exists(targetFile))
                {
                    using (UIImage sourceImage = UIImage.FromFile(sourceFile))
                    {
                        var sourceSize = sourceImage.Size;
                        var maxResizeFactor = Math.Min(maxWidth / sourceSize.Width, maxHeight / sourceSize.Height);

                        if (!Directory.Exists(Path.GetDirectoryName(targetFile)))
                            Directory.CreateDirectory(Path.GetDirectoryName(targetFile));

                        if (maxResizeFactor > 0.9)
                        {
                            File.Copy(sourceFile, targetFile);
                        }
                        else
                        {
                            var width = maxResizeFactor * sourceSize.Width;
                            var height = maxResizeFactor * sourceSize.Height;

                            UIGraphics.BeginImageContextWithOptions(new CGSize((float)width, (float)height), true, 1.0f);
                            //  UIGraphics.GetCurrentContext().RotateCTM(90 / Math.PI);
                            sourceImage.Draw(new CGRect(0, 0, (float)width, (float)height));

                            var resultImage = UIGraphics.GetImageFromCurrentImageContext();
                            UIGraphics.EndImageContext();


                            if (targetFile.ToLower().EndsWith("png"))
                                resultImage.AsPNG().Save(targetFile, true);
                            else
                                resultImage.AsJPEG().Save(targetFile, true);
                        }
                    }
                }
            }
            catch (Exception x)
            {
                //TODO:
            }
        }

When breaking at the catch, it randomly can't find the file. I can't pin down the randomness but the behaviour is the same on the simulator and the device so there's something going on.

Does file path have to be manipulated when using the DependencyService and platform specific code?

Thanks


Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>