Android - 从服务器下载图像并加载到ImageView中

时间:2013-11-01 11:54:36

标签: android

您好我查了类似的问题,但我无法解决问题。它不起作用。 我试图从服务器下载一些图像并将它们放在Android应用程序中的ImageViews中。

public class MyAsyncTask extends AsyncTask {
  protected String doInBackground(String... params)  {
    ...........
    URL urlImagine = new URL("http://s.ytimg.com/yts/img/favicon-vfldLzJxy.ico");
    //Version 1:
    URLConnection conn = urlImagine.openConnection();
    inputStreamImagine = (InputStream)conn.getContent();
    ..........
    //version 2:
    inputStreamImagine = urlImagine.openStream();
    bufImagine = new BufferedInputStream(inputStreamImagine);
    .........
  }
}
    //Some other class:
    .............. 
    //http://stackoverflow.com/questions/15569953/download-image-from-image-url-to-image-view
    //version 1:
    //Download Image From Image URL to image view
    if (inputStreamImagine!=null){
      Bitmap bitmap = BitmapFactory.decodeStream(inputStreamImagine);      
      imageView.setImageBitmap(bitmap);
    }
    //version 2:
    if (bufImagine!=null){
      // Convert the BufferedInputStream to a Bitmap
      Bitmap bMap = BitmapFactory.decodeStream(bufImagine);
      Drawable drawable = new BitmapDrawable(bMap);
      imageView.setBackgroundDrawable(drawable);
      //iv.setImageDrawable(drawable);      
   }

我尝试过使用这两个示例,但ImageView没有加载任何图像。 你能帮我吗?

1 个答案:

答案 0 :(得分:1)

您应该使用延迟加载概念。您应该查看以下链接:

Lazy load of images in ListView