我有一个无法访问已处置的对象。\ n对象名称:' System.Net.Sockets.NetworkStream异常在我尝试将图片从库发送到我的服务器时抛出。
出于贬值的目的,我制作了两种方法,其中一种方法有效,一种方法无效。工作的那个使用一个虚拟字节数组,并且不使用图片Stream对象并从中生成一个字节数组。我将这两种方法都挂钩到OnImagePickerFinishedPickingMedia。
所以我们在共享PCL代码的屏幕上有一个按钮,点击
await DependencyService.Get<IPicturePicker>().GetImageStreamAsync();
仅仅为了调试pruposes我使所有代码都在一个地方发生,因此更容易跟踪错误。在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;
}
这两个方法的部分相同,并且是一些Xamarin示例的完整副本。
现在这里是一个工作方法,并将数组写入db而没有任何问题:
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");
}
}
以下是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");
}
}
如果有人有任何关于为什么会在第二位弹出异常的想法,我会非常感激。到目前为止,我已尝试过多种方法来执行此代码,但尚未找到解决方案。感谢。