2011-11-22 71 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

感謝您的答覆..我試圖使用異步任務。但僅顯示最後一個列表圖像。我會編輯我的答案 –