Xamarin Forms MVVM (Prism) with Media.Plugin - How to get a taken picture from device storage

流过昼夜 提交于 2021-01-27 18:34:47

问题


I am using Xamarin Forms PCL (MVVM Prism) with MediaPlugin (https://github.com/jamesmontemagno/MediaPlugin).

I have a viewModel with a public property called ImagePath

private string imagePath;
public string ImagePath
{
   get { return imagePath; }
   set { SetProperty(ref imagePath, value, "ImagePath"); }
}

in my XAML i have:

<Image Source="{Binding ImagePath}" HeightRequest="100" WidthRequest="100"/>

When i set a image url (from web) to ImagePath property, it works without problems. (I am setting an string like this: "http://www.myaddress.com/myimage.jpg")

But, when I take a picture using the following code (MediaPlugin):

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

ImagePath = file.Path;

// also tried: ImagePath = ImageSource.FromFile(file.Path).toString();

the var file.Path receives a value like this:

"/storage/emulated/0/Android/data/{APPNAME}.Android/files/Pictures/{DIRECTORYNAME}/{IMAGENAME}"

With this file.Path value then i'm setting ImagePath property: ImagePath = file.Path;

But the taken image does not appear on the screen.

I also have tried to use the Image.Source property as suggested in MediaPlugin docs, however i have not reached the result i want.

I also have tried the following:

Public property into VM:

private Image image = new Image();
        public Image Image
        {
            get { return image; }
            set { SetProperty(ref image, value, "Image"); }
        }

Xaml:

<Image Source="{Binding Image.Source}" HeightRequest="100" WidthRequest="100"/>

After set file value (as showed before):

image.Source = ImageSource.FromStream(() =>
                    {
                        var stream = file.GetStream();
                        file.Dispose();
                        return stream;
                    });

Is there any way to make it work? Thanks!!


回答1:


The Media Plugin returns a MediaFile.

I store this in a class I use for storage and display

public class IncidentImage
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }

    public int IncidentDetailsId { get; set; }

    public byte[] ImageBytes { get; set; }

    [Ignore]
    public ImageSource ImageSource
    {
        get
        {
            ImageSource retval = null;
            if (ImageBytes != null)
            {
                retval = ImageSource.FromStream(() => new MemoryStream(ImageBytes));
            }
            return retval;
        }
    }
}

Like this

    var incidentImage = new IncidentImage
    {
        ImageBytes = image.ToByteArray()
    };

image being the MediaFile returned by the plugin.

Then my XAML looks like this

<Image Source="{Binding IncidentImage.ImageSource}" Aspect="AspectFit"  />

You could also do it by moving the ImageSource code to a converter.




回答2:


Hi Steve I do what do you said but, i don't display my photo the first issue y had its the image.ToByteArray()

i resolve it with that code

var memoryStream = new MemoryStream();
file.GetStream().CopyTo(memoryStream);
file.Dispose();

var incidentImage = new IncidentImage
 {
   ImageBytes = memoryStream.ToArray()
 };`


来源:https://stackoverflow.com/questions/47441800/xamarin-forms-mvvm-prism-with-media-plugin-how-to-get-a-taken-picture-from-d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!