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

Cannot access a disposed object exception when trying to send a JSON string made from byte[] pic

$
0
0

Hello! I am having a very hard time with this issue. It seems very strange and I am starting to wonder whether this is actually a Xamarin bug or one og my own. Please help me out if possible.

I have tried very many scenarios with this code and none seemed to work. All of the scenarios were aimed to do the following:

I have two buttons. One button gets a photo from the native gallery and displays it on the screen. The second button is meant to send that photo to the server.

Although I have tried the CrossMedia plugin in step one, I get my photo from the library using the standard Dependancy Service as follows.

private byte[] _globalDataArray;

private async void UploadPhotoButton_OnClicked(object sender, EventArgs e)
{
_globalDataArray = await DependencyService.Get<IPicturePicker>().GetImageStreamAsync();

    if (_globalDataArray != null)
            {
                MemoryStream stream = new MemoryStream(_globalDataArray);
                SelectedImage.Source = ImageSource.FromStream(() => stream);
            }

}

I changed the native code to return the byte[]. It used to return Stream. I thought the error happened because of that but that wasnt the case. Anyway in step two I try and work with the _globalDataArray object and send it over to my web api server which passed all tests. If I send an byte[] that I randomply create here it works fine.

private async void UploadPhotoButton_OnClicked(object sender, EventArgs e)
{
        var gal = new GalleryResource()
            {
                Pic = _globalDataArray
            };

    MemoryObjects.CurrentGallery = gal;
            var ctr = HelperMethods.GetInstance<GalleryController>();
            await ctr.Post();
}

Finally here is the repository part where it all falls:

    public async Task<TEntity> Post(object obj, string endPoint)
    {

        try
        {

    var stringContent = _repService.ConvertObjectToStringContent(obj);

    /**/    exception happens on the line below**
            var response = await _client.PostAsync(endPoint, stringContent  );

            if (await InternalServerError(response))
                return null;

            return _repService.CreateObjectFromHttpResponse<TEntity>(response).Result;
        }
        catch (Exception e)
        {
            await Application.Current.MainPage.DisplayAlert("Repository Error 3",
                Errors.RepositoryError_3 + Environment.NewLine + e.ToString(), "Ok");

            return null;
        }

    }

Really stuck on why this happens...


Viewing all articles
Browse latest Browse all 77050

Trending Articles



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