2014-10-20 81 views
0

我爲kaltura媒體平臺編寫客戶端。 我有一個活動「視頻」。在這個活動中,我必須顯示帶有縮略圖的視頻列表 首先,我使用kaltura api下載視頻列表,並且這工作正常。每個視頻條目都有一個字段thumbnailUrl,它指向視頻的縮略圖。 當我下載視頻縮略圖時,(縮略圖)以不正確的順序下載,某些縮略圖未下載,某些縮略圖重複用於其他視頻,有些未下載。 這是回調的代碼,如果downloadind視頻:如何更正下載多張圖片(縮略圖的視頻)

private void handleFetchvideoListTask(List<VideoObject> videoObjects){ 
     LinearLayout videoListProgressBarContainer = (LinearLayout)mActivity.findViewById(R.id.videoListProgressBarContainer); 
     LinearLayout videoListViewContainer = (LinearLayout)mActivity.findViewById(R.id.videoListViewContainer); 

     videoListProgressBarContainer.setVisibility(View.GONE); 
     videoListViewContainer.setVisibility(View.VISIBLE); 

     ListView listView = (ListView)videoListViewContainer.findViewById(R.id.videoListView); 
     VideoListAdapter adapter = new VideoListAdapter((LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE)); 
     adapter.setList(videoObjects); 
     listView.setAdapter(adapter); 
     DownloadThumbnailsTask task = new DownloadThumbnailsTask(videoObjects, adapter); 
     task.execute(); 
    } 

這是DownloadThumbnailsTask代碼:

public class DownloadThumbnailsTask extends AsyncTask<List<VideoObject>, VideoObject, Void>{ 

     private List<VideoObject> videoObjectList = null; 
     private List<VideoObject> toProcess = null; 
     private VideoListAdapter adapter = null; 

     public DownloadThumbnailsTask(List<VideoObject> videoObjectList, VideoListAdapter adapter){ 
      this.videoObjectList = videoObjectList; 
      this.toProcess = new ArrayList<VideoObject>(videoObjectList); 
      this.adapter = adapter; 
     } 

     @Override 
     protected Void doInBackground(List<VideoObject>... lists) { 

      for(int i=0; i<toProcess.size(); i++){ 
       VideoObject item = videoObjectList.get(i); 
       String urldisplay = item.getThumbnailUrl(); 
       Bitmap mIcon11 = null; 
       try { 
        InputStream in = new java.net.URL(urldisplay).openStream(); 
        mIcon11 = BitmapFactory.decodeStream(in); 
       } catch (Exception e) { 
        Log.e("Error", e.getMessage()); 
        e.printStackTrace(); 
       } 
       item.setThumbnail(mIcon11); 
       mIcon11 = null; 
       publishProgress(item); 
      } 
      return null; 
     } 

     @Override 
     protected void onProgressUpdate(VideoObject... values) { 
      if(values != null && values.length > 0){ 
       VideoObject item = values[0]; 
       videoObjectList.remove(item); 
       videoObjectList.add(item); 
       adapter.setList(videoObjectList); 
       adapter.notifyDataSetChanged(); 
      } 
     } 

     @Override 
     protected void onPostExecute(Void aVoid) { 
      this.toProcess.clear(); 
      this.toProcess = null; 
     } 
    } 

這是適配器的代碼:

public class VideoListAdapter extends BaseAdapter { 

    private List<VideoObject> list; 
    private LayoutInflater layoutInflater; 

    public VideoListAdapter(LayoutInflater layoutInflater){ 
     this.layoutInflater = layoutInflater; 
    } 

    @Override 
    public int getCount() { 
     return list.size(); 
    } 

    @Override 
    public Object getItem(int i) { 
     return list.get(i); 
    } 

    @Override 
    public long getItemId(int i) { 
     return list.get(i).getId(); 
    } 

    @Override 
    public View getView(int i, View view, ViewGroup viewGroup) { 
     if(view == null){ 
      view = layoutInflater.inflate(R.layout.list_item_video, viewGroup, false); 
     } 
     ViewHolder viewHolder = new ViewHolder(view); 
     view.setTag(viewHolder); 
     VideoObject item = getVideoObjectItem(i); 
     viewHolder.duration.setText(item.getFormattedDuration()); 
     viewHolder.title.setText(item.getName()); 
     if(item.getThumbnail() != null){ 
      viewHolder.thumbnail.setImageBitmap(item.getThumbnail()); 
     } 
     return view; 
    } 

    private VideoObject getVideoObjectItem(int position){ 
     return (VideoObject) getItem(position); 
    } 

    public List<VideoObject> getList() { 
     return list; 
    } 

    public void setList(List<VideoObject> list) { 
     this.list = list; 
    } 

    public static class ViewHolder { 
     public final ImageView thumbnail; 
     public final TextView title; 
     public final TextView duration; 

     public ViewHolder(View view) { 
      this.thumbnail = (ImageView)view.findViewById(R.id.imgVideoThumbnail); 
      this.title = (TextView)view.findViewById(R.id.txtVideoName); 
      this.duration = (TextView)view.findViewById(R.id.txtVideoDuration); 
     } 
    } 
} 

這是我的代碼的結果: enter image description here enter image description here

回答

1

我注意到兩兩件事:

  1. doInBackground你正在等待

    InputStream in = new java.net.URL(urldisplay).openStream(); mIcon11 = BitmapFactory.decodeStream(in);

下載下一個圖像之前完成。

試試這個:

protected Void doInBackground(List<VideoObject>... lists) { 
     for(final int i=0; i<toProcess.size(); i++){ 
      try { 
       new Thread(){ 
        @Override 
        public void run() { 
         VideoObject item = videoObjectList.get(i); 
         String urldisplay = item.getThumbnailUrl(); 
         Bitmap mIcon11 = null; 
         try { 
          InputStream in = new java.net.URL(urldisplay).openStream(); 
          mIcon11 = BitmapFactory.decodeStream(in); 
         } catch (Exception e) { 
          Log.e("Error", e.getMessage()); 
          e.printStackTrace(); 
         } 
         item.setThumbnail(mIcon11); 
         publishProgress(item); 
        } 
       }.start(); 
      } catch (Exception e) { 
       Log.e("Error", e.getMessage()); 
      } 
     } 
     return null; 
    } 

    @Override 
    protected void onProgressUpdate(VideoObject... values) { 
     adapter.notifyDataSetChanged(); 
    } 

我覺得這兩條線的問題是:

videoObjectList.remove(item); 
    videoObjectList.add(item); 

你刪除的項目,並在再次探底加入。在設置位圖之後調用notifyDataSetChanged。

  1. Inside getView您每次都創建一個新的ViewHolder。

    @Override 
    public View getView(int i, View view, ViewGroup viewGroup) { 
        ViewHolder viewHolder; 
        if(view == null){ 
         view = layoutInflater.inflate(R.layout.list_item_video, viewGroup, false); 
         viewHolder = new ViewHolder(view); 
         view.setTag(viewHolder); 
        }else{ 
         viewHolder = view.getTag(); 
        } 
        VideoObject item = getVideoObjectItem(i); 
        viewHolder.duration.setText(item.getFormattedDuration()); 
        viewHolder.title.setText(item.getName()); 
        if(item.getThumbnail() != null){ 
         viewHolder.thumbnail.setImageBitmap(item.getThumbnail()); 
        } 
        return view; 
    } 
    
0

你有什麼在你的logcat?也許打開流或解碼失敗。圖像大嗎?

+1

沒有,我在logcat中沒有錯誤,謝謝。 Gonji Dev的解決方案幫助我。 – Alexander 2014-10-20 16:29:18