2011-11-22 147 views
2

我正在尝试创建一个自定义列表适配器,其中包含需要从互联网下载的每个项目的图像。当我第一次进入活动 - 应用程序冻结一段时间,直到下载图像,然后加载活动列表。使用AsyncTask加载图像

因为我可以确定在活动加载之前正在下载图像。如何在加载活动后下载图像。我想我需要使用异步任务。但是由于我将图像加载到自定义阵列适配器中,因此不知道如何去做。

这是我的自定义适配器的getView()

public View getView(int position, View convertView, ViewGroup parent) { 
     View v = convertView; 

     if (v == null) { 
      LayoutInflater vi; 
      vi = LayoutInflater.from(getContext()); 
      v = vi.inflate(R.layout.list_item_default, null); 
     } 

     Item p = items.get(position); 

     if (p != null) { 

      TextView list_title = (TextView) v.findViewById(R.id.list_title); 
      TextView list_description = (TextView) v 
        .findViewById(R.id.list_description); 
      TextView list_timestamp = (TextView) v 
        .findViewById(R.id.list_timestamp); 
      ImageView list_image = (ImageView) v.findViewById(R.id.list_image); 

      if (list_title != null) { 
       list_title.setText(p.getItemTitle()); 
      } 

      if (list_description != null) { 
       list_description.setText(p.getItemDescription()); 
      } 

      if (list_timestamp != null) { 
       list_timestamp.setText(p.getItemTimestamp()); 
      } 

      if (list_image != null) { 
       Log.d("bMobile", "inside getView() image"); 
       try { 
        URL imageURL = new URL(p.getItemImage()); 
        HttpURLConnection con = (HttpURLConnection) imageURL 
          .openConnection(); 
        InputStream inputStrem = con.getInputStream(); 
        Bitmap image = BitmapFactory.decodeStream(inputStrem); 
        if (null != image) 
         list_image.setImageBitmap(image); 
        else 
         Log.d("bMobile", "Bitmap is Null"); 
       } catch (Exception e) { 
       } 
      } 
     } 

     return v; 
    } 

的AsyncTask:

public class DownloadImagesTask extends AsyncTask<ImageView, Void, Bitmap> { 

    ImageView imageView = null; 

    @Override 
    protected Bitmap doInBackground(ImageView... imageViews) { 
     this.imageView = imageViews[0]; 
     return download_Image((String)imageView.getTag()); 
    } 

    @Override 
    protected void onPostExecute(Bitmap result) { 
     imageView.setImageBitmap(result); 
    } 

} 
    private Bitmap download_Image(String url) { 

     Bitmap bmp =null; 
     try{ 
      URL ulrn = new URL(url); 
      HttpURLConnection con = (HttpURLConnection)ulrn.openConnection(); 
      InputStream is = con.getInputStream(); 
      bmp = BitmapFactory.decodeStream(is); 
      if (null != bmp) 
       return bmp; 

      }catch(Exception e){} 
     return bmp; 
    } 
+1

这有蜂问了几次,搜索了Stackoverflow;这里是一个例子http://stackoverflow.com/questions/7729133/using-asynctask-to-load-images-in-listview – C0deAttack

回答

5

“应用程序冻结” - 这是因为你的事件线程或UI线程上下载图像。你永远不应该那样做。所有阻塞操作,长时间运行操作,网络操作都应该在单独的线程上运行。是的,您可以使用asynctask来完成应该解决问题的图像加载。

两个选项,你可以做些什么来解决这个问题是

  1. 使用从机器人福库WebImageView在http://brainflush.wordpress.com/2009/11/23/droid-fu-part-2-webimageview-and-webgalleryadapter/ WebImageView使用缓存,以及所以你不需要重新下载图片,你已经下载。下载图像很昂贵并且会消耗数据。 我使用了WebImageView,并能够在列表中加载图像。我花了大约20分钟才完成所有工作,所以你知道它很容易整合它。

  2. 第二个选项是使用异步任务。请检查Android : Loading an image from the Web with Asynctask。关于如何做到这一点,stackoverflow中有很多信息。

任何时候你的UI挂起,你应该有一种令人毛骨悚然的感觉,你在UI线程上做了什么。

+0

感谢您的答复..我试图使用异步任务。但仅显示最后一个列表图像。我会编辑我的答案 –