2016-01-05 73 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