java图片实时更新_java-使用URL中的图像更新ImageView

在我的android应用程序中,我想使用从URL指定的图像更新ImageView.此URL已作为AsyncTask的一部分通过API提取.

我定义了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后面有空白图像.

在获取产品详细信息之后,作为AsyncTask的doInBackground的一部分,我致电:

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");

}

该图像是从Web加载的,如下所示:

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.

是否有人对此问题有任何想法,或者有将图像从空白图像更新为产品图像的更好方法?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值