发送图像使用QuickBlox聊天SDK - Android电子 [英] Send Image Using QuickBlox Chat SDK - Android

查看:368
本文介绍了发送图像使用QuickBlox聊天SDK - Android电子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个聊天应用程序。对于这个我使用 Quickblox SDK。我与文字聊天进行到现在。现在,我想在聊天过程中发送图像。对于这一点,首先我从SD卡上,然后选择图像上传就成功quickblox服务器,然后使用QBFile参考,我得到的图像ID,并尝试以显示它在聊天窗口中选择图像。

I am working on a Chat Application . For this I am using Quickblox SDk . I have done with text chat till now . Now I am trying to send image in chat . For this , first I select image from SD Card then on image selection it uploads to quickblox server successfully ,then using QBFile reference I get image id and try to show it in chat window .

下面是引用code。

 private void sendChatMessage(String messageText, InputStream imageStream) {

    final QBChatMessage chatMessage = new QBChatMessage();

    //Send Image
    if(imageStream != null){
        File file = FileHelper.getFileInputStream(imageStream, "sample_file.png", "myFile");
        Boolean fileIsPublic = true;
        QBContent.uploadFileTask(file, fileIsPublic, messageText, new QBEntityCallbackImpl<QBFile>() {
            @Override
            public void onSuccess(QBFile qbFile, Bundle params) {

                String publicUrl = qbFile.getPublicUrl();
                Toast.makeText(getApplicationContext(), "Image uploaded success", Toast.LENGTH_SHORT).show();
                id = qbFile.getId();
                Toast.makeText(getApplicationContext(),"Public URl: "+ publicUrl, Toast.LENGTH_SHORT).show();
                Toast.makeText(getApplicationContext(),"ID: "+ id + "", Toast.LENGTH_SHORT).show();

                //
                QBAttachment attach = new QBAttachment("image");
                attach.setId(id + "");
                ArrayList<QBAttachment> arryattach = new ArrayList<QBAttachment>();
                arryattach.add(attach);

                chatMessage.setProperty(PROPERTY_SAVE_TO_HISTORY, "1");
                chatMessage.setDateSent(new Date().getTime() / 1000);
                chatMessage.setBody("");
                chatMessage.setAttachments(arryattach);
                //chatMessage.setId(id+"");

                try {
                    chat.sendMessage(chatMessage);
                } catch (XMPPException e) {
                    Log.e(TAG, "failed to send a message", e);
                } catch (SmackException sme) {
                    Log.e(TAG, "failed to send a message", sme);
                }

                if (dialog.getType() == QBDialogType.PRIVATE) {
                    showMessage(chatMessage);
                }
            }
            @Override
            public void onError(List<String> errors) {
                System.out.println("==========image uploaded Errors++++++++" + errors.toString());
            }
        }, new QBProgressCallback(){
            @Override
            public void onProgressUpdate(int progress){
            }
        });

    }else{
        //Send Text Body.
        chatMessage.setBody(messageText);
        chatMessage.setProperty(PROPERTY_SAVE_TO_HISTORY, "1");
        chatMessage.setDateSent(new Date().getTime() / 1000);

        try {
            chat.sendMessage(chatMessage);
        } catch (XMPPException e) {
            Log.e(TAG, "failed to send a message", e);
        } catch (SmackException sme) {
            Log.e(TAG, "failed to send a message", sme);
        }

        messageEditText.setText("");

        if (dialog.getType() == QBDialogType.PRIVATE) {
            showMessage(chatMessage);
        }
    }

}

getView(中)的 ChatAdapter.java

    @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) {

            if (type == ChatItemType.Sticker.ordinal()) {
                convertView = vi.inflate(R.layout.list_item_sticker, parent, false);
            } else if (type == ChatItemType.Message.ordinal()) {
                convertView = vi.inflate(R.layout.list_item_message, parent, false);
            } else {
                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();
            }
            new BackgroundOperation(holder ,imageid).execute();

        } else if (StickersManager.isSticker(chatMessage.getBody())) {
            StickersManager.with(convertView.getContext())
                    .loadSticker(chatMessage.getBody())
                    .setPlaceholderColorFilterRes(android.R.color.darker_gray)
                    .into(holder.stickerView);
        } else if (holder.txtMessage != null) {
            holder.txtMessage.setText(chatMessage.getBody());
        }

        if (chatMessage.getSenderId() != null) {
            holder.txtInfo.setText(chatMessage.getSenderId() + ": " + getTimeText(chatMessage));
        } else {
            holder.txtInfo.setText(getTimeText(chatMessage));
        }
        return convertView;
    }

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

下面我打电话新BackgroundOperation(持有者,imageid).execute(); 里面getView(),在这里,我通过电流架和imageid从quickblox服务器下载图像。这里发生了什么,当新BackgroundOperation(持有者,imageid).execute()运行(图片上传后),然后返回doInBackground的声明()不执行时,即使图像被下载和我我得到相应的 QBContent.downloadFileTask()。这里的的onSuccess)的InputStream(我很困惑,之所以doInBackground()不回来了。

Here I am calling new BackgroundOperation(holder ,imageid).execute(); inside getView(), Here I am passing current holder and imageid to download image from quickblox server . What is happening here ,when new BackgroundOperation(holder ,imageid).execute() runs ( after image upload ) ,then return statement of doInBackground() doesn't executes ,even image is being downloaded and I am getting corresponding InputStream in onSuccess() of QBContent.downloadFileTask() .Here I am very confused that why doInBackground() is not returning .

推荐答案

试试这个code:

里面sendChatMessage

替换

id = qbFile.getId();

通过

id = qbFile.getUid();

里面ChatAdapter.java的getView()

替换

new BackgroundOperation(holder ,imageid).execute();

通过

String url =prefixofimagePublicurl +imageid ;
Picasso.with(context).load(url).into(imageView);

从ChatAdapter.java删除BackgroundOperation。

Remove BackgroundOperation from ChatAdapter.java .

请注意:imageid INT OT字符串的变化数据类型

Note : Change data type of imageid int ot String

这篇关于发送图像使用QuickBlox聊天SDK - Android电子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆