2012-04-13 95 views
0

我有一個應用程序,我正在其中下載並在線程中顯示圖像。一切工作正常。但是當圖像被加載時,相同的圖像會在兩到三個地方加載,而當我們滾動屏幕時,圖像加載正確。請任何一個人都可以對此提出任何建議?Android:有圖像下載的問題

代碼:

try{ 
    holder.progress.setVisibility(View.VISIBLE); 
    DownLoadImageInAThreadHandler(Item, holder); 

} 
catch(Exception e) 
{ 
    System.out.println("Exception in Downloading image : " + e.getMessage()); 
} 
return convertView; 
} 


//This code is outside the getview method 
public void DownLoadImageInAThreadHandler(final CategoryData Item, final ViewHolder holder) 
{ 
    nImageDownLoads++; 

    System.out.println("The images being downloaded :" + nImageDownLoads); 

    final Handler handler = new Handler() 
    { 
     @Override public void handleMessage(Message message) 
     { 
      holder.imgitem.setImageDrawable((Drawable) message.obj); 
      holder.imgitem.setVisibility(View.VISIBLE); 
      holder.progress.setVisibility(View.GONE); 
     } 
    }; 
    //Thread for downloading the images 
    Thread t = new Thread() 
    { 
     public void run() 
     { 
      try 
      { 
       Item.bImageDownLoaded = 2; 
       System.out.println("Downloading image  :"+Item.ImageUrl); 
       drawable=getDrawableFromUrl(Item.ImageUrl); 
       nImageDownLoads--; 
       System.out.println("Downloaded image :" + Item.ImageUrl); 
       System.out.println("Remaining images for downloading: " + nImageDownLoads); 
       if(drawable != null) 
       {               
        Item.bImageDownLoaded = 1; 
        //Send the message to the handler 
        Message message = handler.obtainMessage(1, drawable); 
        handler.sendMessage(message); 
       } 
       else{ 
        int idNoImage = R.drawable.giftsuggestionsnoimage; 
        Drawable dwgNoImg = getParent().getResources().getDrawable(idNoImage); 

        //Send the message to the handler 
        Message message = handler.obtainMessage(1, dwgNoImg); 
        handler.sendMessage(message); 
       } 
      } 
      catch(Exception exp) 
      { 
       System.out.println("Exception in DownLoadImageInAThread : " + exp.getMessage()); 
      } 
     } 

     private Drawable getDrawableFromUrl(String imageUrl) throws IOException 
     { 
      try 
      { 
       image = DownloadDrawable(imageUrl, "src"); 
       //bmp = readBitmapFromNetwork(new URL(imageUrl)); 
      } 
      catch (IOException e) 
      { 
       e.printStackTrace(); 
      } 
      return image; 
      //return bmp; 
     } 
    }; 
    t.start(); 
} 

Drawable DownloadDrawable(String url, String src_name) throws java.io.IOException 
{ 
    return Drawable.createFromStream(((java.io.InputStream) new java.net.URL(url).getContent()), src_name); 
} 
+0

題外話:你在這裏發佈代碼之前,你能使其「美觀」,例如使用http://www.prettyprinter.de是不可讀(或在Eclipse中使用SHIFT + CTRL + F) – Selvin 2012-04-13 11:52:29

+0

這裏是一個圖書館,將爲您處理所有這些https:/ /github.com/DHuckaby/Prime – HandlerExploit 2012-04-13 12:27:07

回答

0

您是否使用一個ListView?如果是這樣,有可能視圖已被ListView重用(重用來顯示另一個列表項)到在其中寫入圖像的時間?

+0

是的,我正在使用列表活動 – 2012-04-17 05:06:52

+0

另請參閱http://www.youtube.com/watch?v=wDBM6wVEO70和http://stackoverflow.com/questions/541966/android-how我做了一個懶惰的圖像加載列表視圖/ 3068012#3068012。請注意,我使用的WebImageView中所述http://brainflush.wordpress.com/2009/11/23/droid-fu-part-2-webimageview-and-webgalleryadapter/ – 2012-04-17 07:07:03

0

問題發生,因爲您已經編寫了代碼以加載getView()方法中的圖像。

而且每次滾動列表時都會調用該方法。

所以這是一個可憐的,不好的主意。

從那裏刪除代碼.. 加載圖像和其他threadAsyncTask 然後調用你notifyDataSetChanged()Adapter class對象。

僅幾步之遙爲我指示做,請仔細閱讀,並嘗試實施,如果無法理解任何東西隨意ask..sorry想不出任何粘貼代碼片斷..

ListView控件適配器的getView()方法

show default icon if array doesn't contain bitmap at that position 
if bitmap is there show bitmap else show default icon 

AsyncTask to Load Data 

do in background 
load data and show in listview 

AsyncTask to Load Images 

do in background 
download images one bye one from the urls got in first AsyncTask 
in onPostExecute call adapter's notifyDataSetChanged() to show the bitmaps too 

you can also call onProgressUpdate periodically means..after 2-3 image download 
call publishProgress in doInBackground which will call onProgressUpdate and 
in that method also write adpater.notifyDataSetChanged() to 
reflect currently downloaded/available bitmaps in array 
+0

我應該把這段代碼從getview () 方法。我如何設置行的圖像位置? – 2012-04-17 06:17:00

+0

@sravanthiV更新了答案.. – MKJParekh 2012-04-17 07:29:45