【问题标题】:Download image from QuickBlox Server inside BaseAdapter - Android从 BaseAdapter 内的 QuickBlox 服务器下载图像 - Android
【发布时间】:2016-01-05 06:04:09
【问题描述】:

您好,我正在使用 QuickBlox SDK 开发聊天应用程序。到目前为止,我已经完成了文本聊天。现在我正在发送附件(例如仅图像)。为此,我首先在 quickblox Content 部分上传图像,成功上传后,我使用 AsyncTask 在 BaseAdapter 的子类中下载相同的图像。这里发生了什么,doInBackground()的return语句没有执行,所以我想在这里,我在这里做错了什么。请帮帮我。

这是 BaseAdapter 子类的代码 sn-p ,我正在尝试从 quickblox 服务器下载图像。

BaseAdapter 子类的getView()

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

BaseAdapter 子类中的BackgroundOperation

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

【问题讨论】:

  • 你找到合适的解决方案了吗,然后请发布它.. :)
  • @Jatin 是的,我找到了合适的解决方案。我将发布到一天结束。
  • 感谢重播。请发表你的答案;)
  • @Jatin 你可以找到答案here

标签: android android-asynctask baseadapter quickblox


【解决方案1】:

我认为您在异步任务中使用了异步代码。 显然你应该在主 UI 线程中调用 QB 回调,或者如果你想从后台线程运行这个异步代码,你必须创建一个特殊的处理程序。请参阅此处了解说明: http://quickblox.com/developers/Android#Sync_vs_Async

【讨论】:

    猜你喜欢
    • 2015-09-10
    • 2012-09-22
    • 2016-01-21
    • 2017-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多