Hello! This is a second version of this quesion asked in https://forums.xamarin.com/discussion/127951/cannot-access-a-disposed-object-exception-when-trying-to-send-a-json-string-made-from-byte-pic#latest which makes now a bit more sense I hope.
I am having a Cannot access a disposed object.\nObject name: 'System.Net.Sockets.NetworkStream exception thrown any time I try to send a picture from gallery to my server.
For debuging purposes I have made two methods where one works and one doesn't. The one that works uses a dummy byte array and the one that doesn't takes the picture Stream object and makes a byte array from it. I hooked both methods up to OnImagePickerFinishedPickingMedia.
So we have a button on screen in the shared PCL code which calls on click
await DependencyService.Get().GetImageStreamAsync();
Just for debuging pruposes I made all code to happen in one place so its easier to track the bug. Inside the GetImageStreamAsync()
public Task<byte[]> GetImageStreamAsync()
{
// Create and define UIImagePickerController
imagePicker = new UIImagePickerController
{
SourceType = UIImagePickerControllerSourceType.PhotoLibrary,
MediaTypes = UIImagePickerController.AvailableMediaTypes(UIImagePickerControllerSourceType.PhotoLibrary)
};
// Set event handlers
imagePicker.FinishedPickingMedia += OnImagePickerFinishedPickingMedia;
imagePicker.Canceled += OnImagePickerCancelled;
// Present UIImagePickerController;
UIWindow window = UIApplication.SharedApplication.KeyWindow;
var viewController = window.RootViewController;
viewController.PresentModalViewController(imagePicker, true);
// Return Task object
taskCompletionSource = new TaskCompletionSource<byte[]>();
return taskCompletionSource.Task;
}
This part is the same for both methods and is a complete copy of what you can get off some Xamarin examples.
Now here is the method that works and writes an array to db with no issues:
void OnImagePickerFinishedPickingMedia(object sender, UIImagePickerMediaPickedEventArgs args)
{
UIImage image = args.EditedImage ?? args.OriginalImage;
if (image != null)
{
// Convert UIImage to .NET Stream object
NSData data = image.AsJPEG(1);
Stream stream = data.AsStream();
var arrayData = data.ToArray();
try
{
CallMe();
}
catch (Exception e)
{
Xamarin.Forms.Application.Current.MainPage.DisplayAlert("Repository Error 3",
Errors.RepositoryError_3 + Environment.NewLine + e.ToString(), "Ok");
}
taskCompletionSource.SetResult(arrayData);
}
else
{
taskCompletionSource.SetResult(null);
}
imagePicker.DismissModalViewController(true);
}
private async void CallMe()
{
try
{
string xq = "csdcsdc";
byte[] byteArray = Encoding.UTF8.GetBytes(xq);
var gal = new GalleryResource()
{
Pic = byteArray
};
var c = RepositoryService.ConvertObjectToStringContentStatic(gal);
var client = new HttpClient();
var response = await client.PostAsync(Settings.EndPoint + "api/gallery/", c);
}
catch (Exception e)
{
Xamarin.Forms.Application.Current.MainPage.DisplayAlert("Call me!!!",
Errors.RepositoryError_3 + Environment.NewLine + e.ToString(), "Ok");
}
}
Here is the one that FAILS:
private async void CallMe()
{
try
{
string xq = "csdcsdc";
byte[] byteArray = Encoding.UTF8.GetBytes(xq);
var gal = new GalleryResource()
{
Pic = byteArray
};
var c = RepositoryService.ConvertObjectToStringContentStatic(gal);
var client = new HttpClient();
var response = await client.PostAsync(Settings.EndPoint + "api/gallery/", c);
}
catch (Exception e)
{
Xamarin.Forms.Application.Current.MainPage.DisplayAlert("Call me!!!",
Errors.RepositoryError_3 + Environment.NewLine + e.ToString(), "Ok");
}
}
void OnImagePickerFinishedPickingMedia(object sender, UIImagePickerMediaPickedEventArgs args)
{
UIImage image = args.EditedImage ?? args.OriginalImage;
if (image != null)
{
// Convert UIImage to .NET Stream object
NSData data = image.AsJPEG(1);
Stream stream = data.AsStream();
var arrayData = data.ToArray();
try
{
CallMe(arrayData);
}
catch (Exception e)
{
Xamarin.Forms.Application.Current.MainPage.DisplayAlert("Repository Error 3",
Errors.RepositoryError_3 + Environment.NewLine + e.ToString(), "Ok");
}
taskCompletionSource.SetResult(arrayData);
}
else
{
taskCompletionSource.SetResult(null);
}
imagePicker.DismissModalViewController(true);
}
private async void CallMe(byte[] byteArray)
{
try
{
string xq = "csdcsdc";
//byte[] byteArray = Encoding.UTF8.GetBytes(xq);
var gal = new GalleryResource()
{
Pic = byteArray
};
var c = RepositoryService.ConvertObjectToStringContentStatic(gal);
var client = new HttpClient();
var response = await client.PostAsync(Settings.EndPoint + "api/gallery/", c);
}
catch (Exception e)
{
Xamarin.Forms.Application.Current.MainPage.DisplayAlert("Call me!!!",
Errors.RepositoryError_3 + Environment.NewLine + e.ToString(), "Ok");
}
}
If anyone has any ideas on why does an exception pop up in the second place I will be really greatfull. So far I have tried numerous ways to execute this code but have not yet found a solution. Thanks.