如何把一個圖片來自互聯網的ImageView的?
ImageView image = (ImageView) findViewById(R.id.main_layout_image_cover);
image。(?!);
如何把一個圖片來自互聯網的ImageView的?
ImageView image = (ImageView) findViewById(R.id.main_layout_image_cover);
image。(?!);
這裏使用:
URL imageURL = new URL("http://....");
Bitmap image = BitmapFactory.decodeStream(imageUrl.openConnection().getInputStream());
ImageView imageView = (ImageView) findViewById(R.id.main_layout_image_cover);
imageView.setImageBitmap(image);
當然,這將阻止當前線程同時下載,所以你應該使用的AsyncTask
假設你有圖片的URL,這裏是一個代碼示例,可以下載的圖片,並將其分配給ImageView
Drawable drawable = LoadImageFromWebOperations(enclosure.getUrl());
if (drawable!=null)
img.setImageDrawable(drawable);
img.setScaleType(ScaleType.CENTER_CROP); // Used to crop the picture in case it is too big
public Drawable LoadImageFromWebOperations(String url) {
try{
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
}catch (Exception e) {
return null;
}
我會建議你插入在此的AsyncTask代碼,因爲它會降低加載圖片,這樣的操作不打算在主線程上完成。如果您不會添加ASyncTask,則UI將凍結。
太謝謝了^ _ ^ – ruslanys 2012-08-10 17:34:16
謝謝。很有用。 – ruslanys 2012-08-10 16:59:24