I am working on a Android App using Xamarin Forms. I have ImageCircle plugin added on my form to display profile image. I want to update it with the photo captured from the phone's camera. So to do this i have these pieces of code.
1. XAML
<controls:CircleImage x:Name="ImgProfile" BorderColor="DarkSlateGray" BorderThickness="5" Aspect="AspectFit" Scale="0.6" HeightRequest="150" WidthRequest="150" />
<ImageButton Source="pan.png" BackgroundColor="Transparent" Clicked="ImageButton_Clicked"></ImageButton>
2. C#
async void TakePhoto()
{
await CrossMedia.Current.Initialize();
var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
{
PhotoSize = Plugin.Media.Abstractions.PhotoSize.Small,
Name = Guid.NewGuid().ToString().Substring(0,8),
Directory= "profile"
});
if(file==null)
{
return;
}
ImgProfile.Source= ImageSource.FromStream(() =>
{
var stream = file.GetStream();
return stream;
});
}
private void ImageButton_Clicked(object sender, EventArgs e)
{
TakePhoto();
}
I tried below ways as well, but no success:
Setting Source = file.Path
Using Byte Array
- {Binding ImageSource} in xaml and setting imagesource in code behind.
The above code is running fine, I can see byte array/stream as well in watch window.But still, Image is not displaying.
Please Note:
My approach is old school WinForm way not MVVM.
Plugins / Modules version
VS 2019 Community 16.5.5
Xamarin Android SDK - 10.2.0.100
Xamarin.Forms 4.6.0.800
- Xamarin.Plugin.Media 5.0.1
- Xamarin.Plugins.Forms.ImageCircle 3.0.0.5
Thanks in Advance
Image
? – Morse