简体   繁体   中英

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. I have a viewmodel and a page where I use the function and the function itself works fine. 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. You even refer to it in your ViewModel

VideoInfo = mediaFile.Path;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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