Xamarin.Forms ImageSource.FromFile or FromStream not working in Android API 23 device

v7v7 CAMember
edited October 2016 in Xamarin.Forms

Using MediaPlugin (https://github.com/jamesmontemagno/MediaPlugin/tree/master) and the image not showing in Android API 23 device. It works fine in API 16 and 19 devices.

Here's the code I used

        Image image = new Image();            

        Button btnCamera = new Button
        {
            Text = "Take Picture",
            Font = Font.SystemFontOfSize(NamedSize.Large),
            BorderWidth = 1,
            HorizontalOptions = LayoutOptions.Center,
            VerticalOptions = LayoutOptions.CenterAndExpand
        };
        btnCamera.Clicked += async (sender, args) =>
        {
            try
            {
                await CrossMedia.Current.Initialize();

                if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
                {
                    lblNotification.Text = "No Camera";
                    return;
                }

                var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
                {
                    //SaveToAlbum = true,
                    //Directory = "XM",
                    Name = "test.jpg"
                });

                if (file == null)
                {
                    lblNotification.Text = "No Picture Taken";
                    return;
                }

                //lblNotification.Text = file.Path;

                //image.Source = file.Path;
                //image.Source = ImageSource.FromFile(file.Path);
                image.Source = ImageSource.FromStream(() => 
                {
                    var stream = file.GetStream();
                    file.Dispose();
                    return stream;
                });
            }
            catch (Exception ex)
            {
                lblNotification.Text = ex.Message;
            }
        };

I also made a simple page set Image Source to "/storage/emulated/0/Pictures/test.jpg" (confirmed that test.jpg is existing in all devices) and the same thing happens (working in API 16, 19 but not working in API 23). So it's not MediaPlugin issue at all.

Is this a bug? or any suggestion to show image in Android API 23 device after taking a picture?

Thanks in advance.

Best Answer

Answers

  • rr2d2rr2d2 USMember

    In API 22 (I think it is 22) and up you have to ask the user for permissions as you need them. So when you try to use the camera you have to ask for camera permission before you can use it. API's below 22 you state all the permissions you want to use up front (In your manifest file) that's why it is working in API 16 and 19.

  • v7v7 CAMember

    Thanks @rr2d2 for the reply

    The camera works fine, and I can see the taken picture stored in the device. The only issue is Xamarin.Forms doesn't show the taken picture in my ContentPage Image control.

  • Logan.HauptLogan.Haupt USUniversity
    edited November 2016

    @v7 said:
    Thanks @rr2d2 for the reply

    The camera works fine, and I can see the taken picture stored in the device. The only issue is Xamarin.Forms doesn't show the taken picture in my ContentPage Image control.

    Used to be able to run with ImageSource.FromFile(FileName)... and it worked flawless; However, I have had the same problem ever since I updated to 2.3.2.127 (or any version 2.3.0+) (Only reason being able to use the CarouselView). I tried implementing platform specific code to get bytes/stream of the image to recreate in the image source, and this still does not work. I've tried using a standard Image as well as the CachedImage. I may end up exploring in the way the ImageSource now works in this version, to see if any changes have occurred.

    Let me know if you find anything out please.

  • HectorValeroHectorValero USMember

    I have the same problem, but selecting camera resolution < 9 Mpxls it works.
    In android 7 i don't have this problem

    Is possible that in previous versions of android 7 there is a limit size for Image.Source???

  • NamyslawSzymaniukNamyslawSzymaniuk USMember ✭✭✭

    @HectorValero said:
    I have the same problem, but selecting camera resolution < 9 Mpxls it works.
    In android 7 i don't have this problem

    Is possible that in previous versions of android 7 there is a limit size for Image.Source???

    Don't use default Image control, use FFImageLoading plugin, and CachedImage control in it. Really.
    Even if you won't face OOM issue with Image on your device, it can simply crash on some low-end chinese devices.

Sign In or Register to comment.