1

Hi I am working on a Chat Application using QuickBlox SDK . Here I have done with text chat till now . Now I am working on sending attachment (E.g image only) . For this first I am uploading image on quickblox Content section , after successfull upload I download same image inside subclass of BaseAdapter using AsyncTask . What is happening here ,the return statement of doInBackground() doesn't executes ,So here I want ,what wrong I am doing here . Help me please .

Here is the code snippet of subclass of BaseAdapter ,where I am trying to download image from quickblox server .

getView() of subclass of BaseAdapter

     @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;
}

BackgroundOperation inside sub class of 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);
           }
       }
   }
Shishupal Shakya
  • 1,632
  • 2
  • 18
  • 41

1 Answers1

0

I think you're using asyncronous code within an async task. Apparently you should call the QB callback from with in the main UI thread, or you have to create a special handler if you want to run this asynchronous code from a background thread. See here for explanation: http://quickblox.com/developers/Android#Sync_vs_Async