Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android MonoDroid将图像下载到ImageView_Android_Download_Imageview_Xamarin.android - Fatal编程技术网

Android MonoDroid将图像下载到ImageView

Android MonoDroid将图像下载到ImageView,android,download,imageview,xamarin.android,Android,Download,Imageview,Xamarin.android,我刚刚开始使用VisualStudio对MonoDroid进行实验 正在尝试从web下载图像并将其显示在ImageView控件中 不幸的是,由于某种奇怪的原因,图像无法显示在ImageView中。虽然图像似乎已成功下载,但控件仍为空 我做错什么了吗 代码如下: private void DoClick(object sender, EventArgs e) { WebClient web = new WebClient(); web.DownloadDataCompleted

我刚刚开始使用VisualStudio对MonoDroid进行实验

正在尝试从web下载图像并将其显示在ImageView控件中

不幸的是,由于某种奇怪的原因,图像无法显示在ImageView中。虽然图像似乎已成功下载,但控件仍为空

我做错什么了吗

代码如下:

private void DoClick(object sender, EventArgs e)
{

    WebClient web = new WebClient();
    web.DownloadDataCompleted += new DownloadDataCompletedEventHandler(web_DownloadDataCompleted);
    web.DownloadDataAsync(new Uri(@"http://upload.wikimedia.org/wikipedia/commons/d/d9/Test.png"));


    Toast.MakeText(this, "Image downloaded!", ToastLength.Short).Show();
}


void web_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{

    if (e.Error != null)
    {
        Toast.MakeText(this, e.Error.Message, ToastLength.Short).Show();
    }

    else
    {

        Bitmap bm = BitmapFactory.DecodeByteArray(e.Result, 0, e.Result.Length);

        ImageView imgView = FindViewById<ImageView>(Resource.Id.MyImageView);
        imgView.SetImageBitmap(bm);


    }


}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Button  
    android:id="@+id/MyButton"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/Hello"
    />


  <ImageView
    android:id="@+id/MyImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_below="@+id/MyButton"/>



</LinearLayout>
Main.axml的定义如下:

private void DoClick(object sender, EventArgs e)
{

    WebClient web = new WebClient();
    web.DownloadDataCompleted += new DownloadDataCompletedEventHandler(web_DownloadDataCompleted);
    web.DownloadDataAsync(new Uri(@"http://upload.wikimedia.org/wikipedia/commons/d/d9/Test.png"));


    Toast.MakeText(this, "Image downloaded!", ToastLength.Short).Show();
}


void web_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{

    if (e.Error != null)
    {
        Toast.MakeText(this, e.Error.Message, ToastLength.Short).Show();
    }

    else
    {

        Bitmap bm = BitmapFactory.DecodeByteArray(e.Result, 0, e.Result.Length);

        ImageView imgView = FindViewById<ImageView>(Resource.Id.MyImageView);
        imgView.SetImageBitmap(bm);


    }


}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Button  
    android:id="@+id/MyButton"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/Hello"
    />


  <ImageView
    android:id="@+id/MyImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_below="@+id/MyButton"/>



</LinearLayout>

我在这里看到两个问题:

由于下载是异步的,因此在开始下载后将立即调用您下载的图像。如果希望在下载完成后显示,则应将其放置在web_DownloadDataCompleted方法中。 这里的主要问题是您试图从后台线程更新UI。因为下载是异步的,所以它是在后台线程(包括回调)上处理的。但是,您不能从后台线程更新UI。如果将该方法的内容包装在对RunOnUiThread的调用中,代码将按预期工作:

void web_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{

    if (e.Error != null)
    {
        RunOnUiThread(() =>
            Toast.MakeText(this, e.Error.Message, ToastLength.Short).Show());
    }
    else
    {

        Bitmap bm = BitmapFactory.DecodeByteArray(e.Result, 0, e.Result.Length);

        RunOnUiThread(() =>
        {
            ImageView imgView = FindViewById<ImageView>(Resource.Id.MyImageView);
            imgView.SetImageBitmap(bm);
        });
    }
}

我在这里看到两个问题:

由于下载是异步的,因此在开始下载后将立即调用您下载的图像。如果希望在下载完成后显示,则应将其放置在web_DownloadDataCompleted方法中。 这里的主要问题是您试图从后台线程更新UI。因为下载是异步的,所以它是在后台线程(包括回调)上处理的。但是,您不能从后台线程更新UI。如果将该方法的内容包装在对RunOnUiThread的调用中,代码将按预期工作:

void web_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{

    if (e.Error != null)
    {
        RunOnUiThread(() =>
            Toast.MakeText(this, e.Error.Message, ToastLength.Short).Show());
    }
    else
    {

        Bitmap bm = BitmapFactory.DecodeByteArray(e.Result, 0, e.Result.Length);

        RunOnUiThread(() =>
        {
            ImageView imgView = FindViewById<ImageView>(Resource.Id.MyImageView);
            imgView.SetImageBitmap(bm);
        });
    }
}