I followed this guide here https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/dependency-service/photo-picker
async void OnPickPhotoButtonClicked(object sender,EventArgs e)
{
(sender as Image).IsEnabled = false;
Stream stream = await DependencyService.Get<IPhotoPickerService>().GetImageStreamAsync();
if(stream != null)
{
temp_photo.Source = ImageSource.FromStream(() => stream);
}
(sender as Image).IsEnabled = true;
}
When I select an image using this it will show the image from the gallery in the
What I need to do is convert the image to a base64_string so that it can be sent to our server
I tried following the accepted answer from this link https://forums.xamarin.com/discussion/81344/how-to-convert-image-from-plugin-media-to-base64
var stream = file.GetStream();
var bytes = new byte [stream.Length];
await stream.ReadAsync(bytes, 0, (int)stream.Length);
string base64 = System.Convert.ToBase64String(bytes);
Adopting my method to look like this
async void OnPickPhotoButtonClicked(object sender,EventArgs e)
{
(sender as Image).IsEnabled = false;
Stream stream = await DependencyService.Get<IPhotoPickerService>().GetImageStreamAsync();
if(stream != null)
{
temp_photo.Source = ImageSource.FromStream(() => stream);
var bytes = new byte[stream.Length];
await stream.ReadAsync(bytes, 0, (int)stream.Length);
string base64 = System.Convert.ToBase64String(bytes);
}
(sender as Image).IsEnabled = true;
}
But upon selecting an image this causes the application to crash
How can I convert the image selected into a base64 string?
For reference I am testing on an android device
Edit: Wrapped my method in a try block to catch the exception and this is what it says
Specified method is not supported.