i'm simply trying to upload image to server.
when i choose image from , i get URI to that image.
the question is how can i convert this URI to byte[]
byte array?
no more no less. thats my question
this is what ive been trying.
i tried to rewrite this https://colinyeoh.wordpress.com/2012/05/18/android-convert-image-uri-to-byte-array/ to 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 this? or new code to get image from gallery and convert that to byte array is fine.
help!
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With