3

I have made a demo for sending image to private chat using QuickBlox, I am struggling to attach an image with the chat message, I have gone through its document and have used the below Links, with no luck Attach an image

My code is as below:

chatMessage = new QBChatMessage();

sendButton = (Button) findViewById(R.id.chatSendButton);
sendButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        final String messageText = messageEditText.getText().toString();
        if (TextUtils.isEmpty(messageText)) {
            return;
        }

        // Send chat message
        //

        // send a message
        // ...
        int fileId = R.raw.ic_launcher;
        InputStream is = ChatActivity.this.getResources()
                        .openRawResource(fileId);
        File file = FileHelper.getFileInputStream(is,
                        "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();
                        System.out
                                        .println("==========image uploaded success++++++++"
                                                + publicUrl);

                         id = qbFile.getId();

                        System.out
                                        .println("===================image id+++++++++++"
                                                + id + "");

                    }

                    @Override
                    public void onError(List<String> errors) {
                        System.out
                                        .println("==========image uploaded Errors++++++++"
                                                + errors.toString());

                    }
                }, new QBProgressCallback() {
                    @Override
                    public void onProgressUpdate(int progress) {

                    }
                });
        QBAttachment atach = new QBAttachment("image");
                atach.setId(id+"");


        ArrayList<QBAttachment> aryatch = new ArrayList<QBAttachment>();
        aryatch.add(atach);
        chatMessage.setAttachments(aryatch);
        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);
        }

    }

});
Vladimir Markeev
  • 654
  • 10
  • 25
jigar jims
  • 129
  • 1
  • 12

1 Answers1

1

Well it clear where the mistake is

your id is null here

atach.setId(id+"");

because it will != nil only in onSuccess block of uploadFileTask

So the right way is to forward all attachments logic inside onSuccess block of uploadFileTask

Because these QuickBlox request are asynchronous

Rubycon
  • 18,156
  • 10
  • 49
  • 70
  • I am working on quickblox chat sdk , I am getting a problem while working with this , I have posted a question regarding this , here is the link http://stackoverflow.com/questions/34592262/send-image-using-quickblox-chat-sdk-android . Help Plz – Shishupal Shakya Jan 04 '16 at 13:34