我使用XLABS从图库中选择图像。如何获取图像的文件路径?

时间:2016-02-03 01:51:00

标签: c# image xamarin xamarin.forms

在我的xamarin表单项目中使用XLABS,我尝试访问图像的基础数据,但我不知道如何使用我当前的代码获取它。我有一个viewmodel和一个我使用该函数的页面,函数本身工作正常。我可以选择一张图片并获得它。但是我想获得路径/ filedata。

我的观点模型:

public ImageSource ImageSource
    {
        get { return _ImageSource; }
        set { SetProperty (ref _ImageSource, value); }
    }

private byte[] imageData;

    public byte[] ImageData { get { return imageData; } }

    private byte[] ReadStream(Stream input)
    {
        byte[] buffer = new byte[16*1024];
        using (MemoryStream ms = new MemoryStream())
        {
            int read;
            while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
            {
                ms.Write(buffer, 0, read);
            }
            return ms.ToArray();
        }
    }

    public async Task SelectPicture()
    {
        Setup ();

        ImageSource = null;


        try
        {
            var mediaFile = await _Mediapicker.SelectPhotoAsync(new CameraMediaStorageOptions
                {
                    DefaultCamera = CameraDevice.Front,
                    MaxPixelDimension = 400
                });

            VideoInfo = mediaFile.Path;
            ImageSource = ImageSource.FromStream(() => mediaFile.Source);

        }
        catch (System.Exception ex)
        {
            Status = ex.Message;
        }
    }


    private static double ConvertBytesToMegabytes(long bytes)
    {
        double rtn_value = (bytes / 1024f) / 1024f;

        return rtn_value;
    }

我使用它的页面:

MyViewModel photoGallery = null;

photoGallery = new MyViewModel ();

private async void btnPickPicture_Clicked (object sender, EventArgs e)
    {
        await photoGallery.SelectPicture (); 
        imgPicked.Source = photoGallery.ImageSource; //imgPicked is my image x:name from XAML.

    }

1 个答案:

答案 0 :(得分:1)

MediaFile has a Path property. You even refer to it in your ViewModel

VideoInfo = mediaFile.Path;