Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

CrossMedia Plugin is calling async twice and app crashes

$
0
0

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

Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>