Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

CrossMedia Plugin is calling async twice and app crashes

Hi,

Greetings!

I have came cross this issue recently which was working fine for me until sometime back. I am trying to add a profile pic selected from device or a camera. but the CrossMedia.Current.PickPhotoAsync(mediaOptions) is getting called twice and getting the error " Only one Operation can be active at once".

Error line:

** var selectedImageFile = await CrossMedia.Current.PickPhotoAsync(mediaOptions);**

private async void Upload_Pic(object sender, EventArgs e)
{
await CrossMedia.Current.Initialize();
if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsPickPhotoSupported)
{
await DisplayAlert("Error", "This is not supported on your device", "Ok");
return;
}

        var mediaOptions = new PickMediaOptions()
        {
            PhotoSize = PhotoSize.Medium

        };

               ** var selectedImageFile = await CrossMedia.Current.PickPhotoAsync(mediaOptions);**

                if (selectedImageFile == null)
                {
                    await DisplayAlert("Error", "This is not supported on your device", "Ok");
                    return;
                }
                //selectedImage.Source = ImageSource.FromStream(() => selectedImageFile.GetStream());

                UploadImage(selectedImageFile.GetStream());
            }
    }
    private async void UploadImage(Stream stream)
    {
        var account = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=sevencloudstorage;AccountKey=OrDndpPV9uOk2uR4CXSHmcnTWvy/0hEsZ6IWPxdFMHIOMfr6U2NfpfhP12sEVgM4G8fCn3cFiTN2ALmS1mDRFA==;EndpointSuffix=core.windows.net");
        var client = account.CreateCloudBlobClient();
        var container = client.GetContainerReference("sevenimages");
        await container.CreateIfNotExistsAsync();
        var name = Guid.NewGuid().ToString();
        var blockBlob = container.GetBlockBlobReference($"{name}.jpg");
        await blockBlob.UploadFromStreamAsync(stream);
        string url = blockBlob.Uri.OriginalString;
    }

Answers

  • JohnHardmanJohnHardman GBUniversity admin

    @Mahesh453261

    This is typically seen with handling button presses/taps, but can happen in many scenarios.

    The typical example is where a button press results in a new page being pushed onto the navigation stack. People find that the same page is being pushed twice, simply because the user unknowingly taps the button twice in quick succession. The double page push problem is easily solved (you'll find many posts about it in the forum).

    In your particular case, you need to add your own protection around usage of the CrossMedia plugin, to ensure that calls such as PickPhotoAsync are not made if the previous call to that method has not yet completed.

  • JarvanJarvan Member, Xamarin Team Xamurai

    the CrossMedia.Current.PickPhotoAsync(mediaOptions) is getting called twice and getting the error " Only one Operation can be active at once"

    I tested the plugin in a basic demo, it works fine. To fix this issue, try adding a bool property to detect if the user is picking a photo.

    bool _loaded = false;
    private async void Upload_Pic(object sender, EventArgs e)
    {
        if (_loaded) return;
        _loaded = true;
    
        var file = await Plugin.Media.CrossMedia.Current.PickPhotoAsync(new Plugin.Media.Abstractions.PickMediaOptions
        {
            ...
        });
        ...
        _loaded = false;
    }
    
Sign In or Register to comment.