更新与一个图象的一个的ImageView从URL [英] Updating an ImageView with an image from a URL

查看:90
本文介绍了更新与一个图象的一个的ImageView从URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Andr​​oid应用程序,我想更新的ImageView,从URL指定的图像。这个URL已经通过API被拿来作为AsyncTask的一部分。

Within my android application, I'm wanting to update an ImageView, with an image specified from a URL. This URL has been fetched via an API as part of an AsyncTask.

我已经定义了ImageView的如下:

I have defined the ImageView as follows:

<ImageView
    android:adjustViewBounds="true"
    android:scaleType="fitCenter"
    android:layout_height="wrap_content" 
    android:id="@+id/product_image"
    android:src="@drawable/no_product_image" 
    android:layout_marginRight="6dip"
    android:layout_width="wrap_content"
    />

这样一来,虽然数据加载,后面有一个ProgressDialog空白图像present。

That way, whilst the data is loading, there is a blank image present behind a ProgressDialog.

由于AsyncTask的的doInBackground的一部分,获取产品的详细信息后,我呼吁:

As part of the doInBackground of the AsyncTask, after fetching the product details, I call:

private void createProductScreen(Product product){
        Log.i(TAG, "Updating product screen");

        //Set the product image
        ImageView imgView =(ImageView)findViewById(R.id.product_image);
        Drawable drawable = loadImageFromWeb(product.getImageUrl());
        imgView.setImageDrawable(drawable);
        Log.i(TAG, "Set image");
}

图片被从网络装载如下:

The image is loading from the web as follows:

private Drawable loadImageFromWeb(String url){
        Log.i(TAG, "Fetching image");
        try{
            InputStream is = (InputStream) new URL(url).getContent();
            Drawable d = Drawable.createFromStream(is, "src");
            Log.i(TAG, "Created image from stream");
            return d;
        }catch (Exception e) {
            //TODO handle error
            Log.i(TAG, "Error fetching image");
            System.out.println("Exc="+e);
            return null;
        }
    }

在此运行我看到的创建的映像,从流的日志中,却看不到的设置图像的和的AsyncTask的onPostExecute不会被调用

When this runs I see "Created image from stream" in the log, but don't see "Set image" and the onPostExecute of the AsyncTask is never called.

有没有人有这个问题的任何想法或从空白图像更新形象的产品形象更好的办法?

Does anyone have any ideas on the issue or a better way of updating the image from the blank image to the product image?

推荐答案

您应该得到一个例外,因为您要设置的ImageView在一个独立的线程。 imgView.setImageDrawable(绘制); 必须调用同一个UI线程。 由于您使用的是AsyncTask的类,正确的地方设置ImageView的对象是onPostExecute方法内。 改变你的逻辑的方式,在doInBackground将返回绘制对象对象的onPostExecute方法,然后用它调用 imgView.setImageDrawable(绘制);

You should get an Exception since you are trying to set the ImageView in a separated thread. imgView.setImageDrawable(drawable); must be called in the same UI thread. Since you are using the AsyncTask class, the right place for set the imageview object is inside the onPostExecute method. Change you logic in a way that the doInBackground will returns the Drawable object to the onPostExecute method, then use it calling imgView.setImageDrawable(drawable);

这篇关于更新与一个图象的一个的ImageView从URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆