简体   繁体   English

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

[英]I use XLABS to pick images from a gallery. How to get the filepath of the image?

Using XLABS in my xamarin forms project and I try to reach the underlying data of the image but I am not sure how to get it with my current code. 在我的xamarin表单项目中使用XLABS,我尝试获取图像的基础数据,但是我不确定如何用当前代码获取它。 I have a viewmodel and a page where I use the function and the function itself works fine. 我有一个ViewModel和一个页面,我可以在其中使用该函数,并且函数本身可以正常工作。 I can pick an image and get it. 我可以选择图像并获取它。 But I want to get the path/filedata. 但是我想获取路径/文件数据。

My viewmodel: 我的视图模型:

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;
    }

My page where I use it: 我使用它的页面:

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.

    }

MediaFile has a Path property. MediaFile具有Path属性。 You even refer to it in your ViewModel 您甚至可以在ViewModel中引用它

VideoInfo = mediaFile.Path;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM