locked
Get image stream after selecting it from Gallery RRS feed

  • Question

  • User34503 posted

    Hi Guys,

    I am writing an application using Xamarin.Forms for Android.

    I need to have functionality to select an image and pass it to server (somehow).

    So, the flow will be the following:

    1) User presses button 2) User selects an image from gallery (or camera) 3) Application sends image to server (WebAPI)

    My problem is that I can't find a way to get the file content (bytes or stream) to pass it to server.

    How can I access the file stream to send it to Server?

    Thanks for your help!

    Tuesday, July 26, 2016 7:30 AM

Answers

  • User52634 posted

    Hello @IlliaGryshkevych,

    Try using xamarin.forms lab. https://github.com/XLabs/Xamarin-Forms-Labs/wiki/Camera

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

    mediaFile.Source is your stream, hope it helps.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, July 27, 2016 7:49 AM

All replies

  • User192248 posted

    var file = Your file path;

    using (var memoryStream = new MemoryStream()) { file.GetStream().CopyTo(memoryStream); file.Dispose(); return memoryStream.ToArray(); }

    Tuesday, July 26, 2016 7:33 AM
  • User34503 posted

    @MysuraReddy said: var file = Your file path;

    using (var memoryStream = new MemoryStream()) { file.GetStream().CopyTo(memoryStream); file.Dispose(); return memoryStream.ToArray(); }

    Hi, Mysura,

    Thanks for you suggestion. This won't work because it doesn't give me access to file.

    I have tried to use Java.IO.File object to read the content of file, but it was unable to access the file.

    Tuesday, July 26, 2016 7:46 AM
  • User226261 posted

    Hi @lIlliaGryshkevych check this function

    ` public static async Task GetStreamStringFromImageSourceAsync(StreamImageSource imageSource, CancellationToken cancellationToken = default(CancellationToken)) { if (imageSource.Stream != null) { var stream = await imageSource.Stream(cancellationToken);

                //stream?.Length > 0 can do this - Pubudu
                // stream
                if (stream == null || stream.Length <= 0) return null;
    
                var bytes = new byte[stream.Length];
                stream.Position = 0;
                stream.Read(bytes, 0, (int)stream.Length);
                return Convert.ToBase64String(bytes);
            }
            return null;
        }
    

    `

    Tuesday, July 26, 2016 9:36 PM
  • User226261 posted

    function should return string

    public static async Task string GetStreamStringFromImageSourceAsync(StreamImageSource imageSource, CancellationToken cancellationToken = default(CancellationToken))

    I cannot use "<>"

    Tuesday, July 26, 2016 10:50 PM
  • User34503 posted

    @PubuduGayan said: function should return string

    public static async Task string GetStreamStringFromImageSourceAsync(StreamImageSource imageSource, CancellationToken cancellationToken = default(CancellationToken))

    I cannot use "<>"

    Hi Pubudu,

    Thanks for your suggestions.

    I don't have a StreamImageSource. I have only Uri provided by SelectImageFromGallery intent.

    How can I get a Stream?

    Wednesday, July 27, 2016 7:08 AM
  • User52634 posted

    Hello @IlliaGryshkevych,

    Try using xamarin.forms lab. https://github.com/XLabs/Xamarin-Forms-Labs/wiki/Camera

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

    mediaFile.Source is your stream, hope it helps.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, July 27, 2016 7:49 AM
  • User34503 posted

    @ashalva said: Hello @IlliaGryshkevych,

    Try using xamarin.forms lab. https://github.com/XLabs/Xamarin-Forms-Labs/wiki/Camera

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

    mediaFile.Source is your stream, hope it helps.

    Thanks Shalva,

    It does look like what I was looking for.

    Let me try it!

    Wednesday, July 27, 2016 9:00 AM