C# HttpClient在Xamarin/Android应用程序中不工作
我试图在Xamarin/Android中创建一个示例应用程序,它从Internet下载一个图像并在ImageView中显示。但在执行C# HttpClient在Xamarin/Android应用程序中不工作,c#,xamarin,xamarin.android,httpclient,xamarin.forms,C#,Xamarin,Xamarin.android,Httpclient,Xamarin.forms,我试图在Xamarin/Android中创建一个示例应用程序,它从Internet下载一个图像并在ImageView中显示。但在执行var imageContent=wait httpClient.GetByteArrayAsync(ImageUrl)之后不久UI/应用程序挂起。没有回电回应。我正在添加我的完整源代码供您参考。请帮我看看我的样品有什么问题 [Activity (Label = "ImageDownloadSample", MainLauncher = true, Icon = "
var imageContent=wait httpClient.GetByteArrayAsync(ImageUrl)之后不久代码>UI/应用程序挂起。没有回电回应。我正在添加我的完整源代码供您参考。请帮我看看我的样品有什么问题
[Activity (Label = "ImageDownloadSample", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
private const string ImageUrl = "http://www.olympusimage.com.sg/content/000006422.jpg";
private ImageView imgView;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
SetContentView (Resource.Layout.Main);
var button = FindViewById<Button> (Resource.Id.downloadImage);
imgView = FindViewById<ImageView>(Resource.Id.imageView);
button.Click+=((sender, e) =>
DownloadImageAsync());
}
private async void DownloadImageAsync()
{
var httpClient = new HttpClient ();
imgView.SetImageResource (Android.Resource.Drawable.IcMenuGallery);
var imageContent = await httpClient.GetByteArrayAsync (ImageUrl);
var documentsPath = System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal);
string localFilename = "mytestImage.jpg";
string localPath = System.IO.Path.Combine (documentsPath, localFilename);
File.WriteAllBytes (localPath, imageContent);
var localImage = new Java.IO.File (localFilename);
if (localImage.Exists ()) {
var bitmapImage = BitmapFactory.DecodeFile (localImage.AbsolutePath);
imgView.SetImageBitmap (bitmapImage);
}
}
}
[活动(Label=“ImageDownloadSample”,MainLauncher=true,Icon=“@drawable/Icon”)]
公共课活动:活动
{
私有常量字符串ImageUrl=”http://www.olympusimage.com.sg/content/000006422.jpg";
私有图像视图imgView;
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
var button=findviewbyd(Resource.Id.downloadImage);
imgView=FindViewById(Resource.Id.imageView);
按钮。单击+=((发件人,e)=>
下载ImageAsync());
}
私有异步void下载ImageAsync()
{
var httpClient=newhttpclient();
imgView.SetImageResource(Android.Resource.Drawable.IcMenuGallery);
var imageContent=wait httpClient.GetByteArrayAsync(ImageUrl);
var documentsPath=System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
字符串localFilename=“mytestImage.jpg”;
字符串localPath=System.IO.Path.Combine(documentsPath,localFilename);
File.writealBytes(localPath、imageContent);
var localImage=new Java.IO.File(localFilename);
如果(localImage.Exists()){
var bitmapImage=BitmapFactory.DecodeFile(localImage.AbsolutePath);
imgView.SetImageBitmap(位图图像);
}
}
}
}
请提供帮助他们有一个示例应用程序,可以完全做到这一点:
另外,您尝试下载的图像看起来非常大……我肯定会得到一些更合理的东西,以便在尝试让事情正常运行时排除这一错误。您可能需要将事件处理程序设置为:
button.Click+=(async (sender, e) => DownloadImageAsync());
为了表明它包含可以异步运行的代码,我有同样的问题。检查异常!如果您设置了HttpClient.timeout,则会更大 该示例是通过WebClient实现的。我正在尝试使用最新的HttpClient。为什么我需要坚持旧方法…?你可能不需要。你能更新这个问题来说明你的应用程序挂在哪一行吗?是否有任何内容写入控制台?您还应使用较小的图像。我认为你不应该将8MB图像加载到移动应用程序中。