2016-01-05 75 views
1

你好我的工作使用QuickBlox SDK聊天應用。在這裏,我完成了文本聊天至今。現在我正在發送附件(僅限於圖片)。有關quickblox內容部分這首我上傳圖片,上傳全成之後我使用的AsyncTask下載同一圖像內BaseAdapter的子類。這裏發生了什麼,doInBackground的return語句()不執行,所以在這裏我想,有什麼不對,我在這裏做什麼。請幫幫我 。下載圖像 - Android電子

這裏是BaseAdapter的子類的代碼片段,我試圖從quickblox服務器下載圖片。子類BaseAdapter

class BackgroundOperation extends AsyncTask<InputStream , Void , InputStream>{ 

     ViewHolder holder ; 
     int imageid ; 
     InputStream inputStream; 

     BackgroundOperation(ViewHolder holder , String imageid){ 
      this.holder = holder ; 
      this.imageid = Integer.parseInt(imageid); 
     } 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
     } 

     @Override 
     protected InputStream doInBackground(InputStream... params) { 

      Handler mHandler = new Handler(Looper.getMainLooper()); 
       mHandler.post(new Runnable() { 
        public void run() { 
         QBContent.downloadFileTask(imageid, new QBEntityCallbackImpl<InputStream>() { 
          @Override 
          public void onSuccess(InputStream inputS, Bundle params) { 

           inputStream = inputS ; 

           //ImageView img = holder.image_attachment ; //.setImageDrawable(d); 
           //Toast.makeText(context, "Image download Sucess", Toast.LENGTH_SHORT).show(); 

          } 

          @Override 
          public void onError(List<String> errors) { 
           Log.d("Image Download Error : ", errors.toString()); 
           //Toast.makeText(context, "Image Download Error ", Toast.LENGTH_SHORT).show(); 
          } 
         }, new QBProgressCallback() { 
          @Override 
          public void onProgressUpdate(int progress) { 
           //Toast.makeText(context, "Image Download Progress ", Toast.LENGTH_SHORT).show(); 
          } 
         }); 
        } 
       }); 

      return inputStream; 
     } 

     @Override 
     protected void onPostExecute(InputStream s) { 
      super.onPostExecute(s); 

      if(s != null){ 
       Log.d("InputStream Value :", "****************************"+s.toString()+"******************"); 
       Bitmap bmp = BitmapFactory.decodeStream(s); 
       Drawable d = new BitmapDrawable(context.getResources(), bmp); 
       if(holder.image_attachment != null) 
        holder.image_attachment.setImageDrawable(d); 
      } 
     } 
    } 
+0

您是否找到適當的解決方案,然後請張貼.. :) – Jatin

+0

@Jatin是的,我找到了適當的解決方案。我會發帖直到一天結束。 –

+0

感謝您的重播。請發表您的回答;) – Jatin

回答

0

內BaseAdapter

BackgroundOperation的
 @Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    final ViewHolder holder; 
    QBChatMessage chatMessage = getItem(position); 
    LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    int type = getItemViewType(position) ; 
    if (convertView == null) { 
     convertView = vi.inflate(R.layout.list_item_image, parent, false); 
     holder = createViewHolder(convertView); 
     convertView.setTag(holder); 
    } else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    QBUser currentUser = ChatService.getInstance().getCurrentUser(); 
    boolean isOutgoing = chatMessage.getSenderId() == null || chatMessage.getSenderId().equals(currentUser.getId()); 
    setAlignment(holder, isOutgoing); 

    Collection<QBAttachment> attachments = chatMessage.getAttachments(); 

    //attachments. 
    if (attachments != null && attachments.size() > 0) { 
     String imageid="" ; 
     for(QBAttachment attachment :attachments){ 
      imageid = attachment.getId(); 
     } 
     //Here is the AsyncTask where I am trying to download image 
     new BackgroundOperation(holder ,imageid).execute(); 
    } 

    return convertView; 
} 

子類的

getView(),我認爲您使用的是異步任務中asyncronous代碼。 顯然,您應該在主UI線程中調用QB回調,或者如果要從後臺線程運行此異步代碼,您必須創建一個特殊處理程序。看到這裏解釋: http://quickblox.com/developers/Android#Sync_vs_Async