I'm developing an application in Xamarin forms and I'm needing a feature where the user can choose an image from the gallery and that image can be displayed in an Image control in the application. I'm working with MediaPlugin and I've also tried FilePicker , but so far none of them have worked for me. I need a good hint for how I can work with images in xamarin forms.
FILEPICKER CODE
Calling the method
public async void SelecionarImagem(object sender, EventArgs e)
{
try
{
carregarImagem.CarregaImagem();
imgAgencia.Source = carregarImagem.fileNameFoto;
if (!string.IsNullOrEmpty(carregarImagem.fileDataImage.FileName))
{
imgAgencia.Source = carregarImagem.fileDataImage.FileName;
await DisplayAlert("Exception", carregarImagem.fileDataImage.FileName, "OK");
}
}
catch (Exception ex)
{
await DisplayAlert("Exception", ex.Message, "OK");
}
}
Implementation
public FileData fileDataImage { get; set; }
public async void CarregaImagem()
{
FileData filedata = await CrossFilePicker.Current.PickFile();
while (fileDataImage.FileName.Equals(null))
{
continue;
}
fileDataImage = filedata;
}
This always gives an exception. I made my debug and saw that it tries to assign the result of the selection even before I select an image from the gallery.