Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET 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;
    }

Best Answers

  • JohnHardmanJohnHardman GBUniversity admin
    Accepted Answer

    @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.

  • YelinzhYelinzh Member, Xamarin Team Xamurai
    Accepted Answer

    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;
    }
    

Answers

  • JohnHardmanJohnHardman GBUniversity admin
    Accepted Answer

    @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.

  • YelinzhYelinzh Member, Xamarin Team Xamurai
    Accepted Answer

    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;
    }
    
  • samAdvanESsamAdvanES Member ✭✭

    In my case, this line of code below, ALWAYS causes my app to crash!! Even calling it once for the very first time!
    And Google shows that this problem already existed since 2016, or maybe even earlier, with no apparent solution to it!
    And the funny thing is this nuget, Xam.Plugin.Media, is used by almost 3 Million people!

    var selectedImageFile = await CrossMedia.Current.PickPhotoAsync(mediaOptions);
    
Sign In or Register to comment.