Xamarin image android URI for byte array

I am just trying to upload an image to the server.

when i select an image from i get the URI to that image. the question is, how can I convert this URI to a byte[]

byte array ?

more not less. here is my question

that's what they tried to do.

I tried to rewrite this https://colinyeoh.wordpress.com/2012/05/18/android-convert-image-uri-to-byte-array/ in C #

    public byte[] convertImageToByte(Android.Net.Uri uri)
    {
        byte[] data = null;
        try
        {

            ContentResolver cr = this.ContentResolver;
            var inputStream = cr.OpenInputStream(uri);
            Bitmap bitmap = BitmapFactory.DecodeStream(inputStream);
            var baos = new ByteArrayOutputStream();
            bitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, baos);
            data = baos.ToByteArray();
        }
        catch (FileNotFoundException e)
        {
            e.PrintStackTrace();
        }
        return data;
    }

      

but the error ...

Error CS1503: Argument `#3' cannot convert `Java.IO.ByteArrayOutputStream' expression to type `System.IO.Stream' (CS1503) (Foodle.Droid)

      

How to fix it? or new code to get image from gallery and convert it to byte array.

help!

+3


source to share


1 answer


public byte[] convertImageToByte(Android.Net.Uri uri)
{
    Stream stream = ContentResolver.OpenInputStream(uri);   
    byte[] byteArray;

    using (var memoryStream = new MemoryStream())
    {
       stream.CopyTo(memoryStream);
       byteArray = memoryStream.ToArray();
    }
    return byteArray;
}

      



+1


source







All Articles