在我的android应用程序中,我想更新ImageView,并使用URL中指定的图像。该URL已通过API作为AsyncTask的一部分提取。使用URL中的图像更新ImageView
我已经定义了的ImageView如下:
<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");
}
的图像从网页加载如下:
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。
有没有人有任何想法或更好的方式从空白图像更新到产品图像?
http://www.androidhive.info/2012/07/android-loading-image-from-url-http/关注此帖。我认为你需要添加一些自定义类和从中可以实现的功能来加载图像或图像。 – ishtiyaqm15 2012-12-08 23:00:19