Xamarin Forms - Select and display Images

Asked

Viewed 558 times

0

I am developing an application in Xamarin Forms and I am in need of a functionality in which the user can choose an image from the gallery and that this image can be displayed in an Image control in the application. I’m working with the Mediaplugin and have also experienced the Filepicker, but so far none of them have worked for me. So I need a good tip to know how to work with images in Xamarin Forms.

FILEPICKER

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 one always gives an exception. I did my debug and saw that it tries to assign the result of the selection even before I select an image from the gallery.

  • If you are facing a specific error post your code so we can help you.

  • "but so far none of them has worked for me, "it doesn’t work for what reason ?

  • amigo @Leonardo silva could post the code, the error you are getting? Your question is very poor information! : ) We are waiting!

  • Take a look at the code I use with Mediaplugin to open the photo album, it works perfectly //var file = await Plugin.Media.Crossmedia.Current.Pickasyphotonc(new Plugin.Media.Abstractions.Pickmediaoptions //{ // Photosize = Plugin.Media.Abstractions.PhotoSize.Small //});

  • See if it helps you!

  • Thanks, I’ll take a look here.

  • @Leonardosilva managed to solve?

  • Good morning! Not yet Milton. I’m testing the Filepicker code, but is making an exception when I will make the image selection. Gives an exception message before the image selection screen even appears. FileData filedata = await CrossFilePicker.Current.PickFile(); This code is what calls the image selector, but the "await" command is not being respected and the application follows the execution. Regarding the Mediaplugin, I will rewrite the test application, because the one I am testing is not rendering the start screen.

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.