Image not attached in quickblox for android

I did a demo for sending an image to a private chat using QuickBlox, I am struggling to attach an image with a chat message, I went through his document and used the links below, no luck Attach image

My code looks like this:

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

    }

});

      

+3


source to share


1 answer


Well, it's clear where the error is

your id is null here

atach.setId(id+"");

      



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

So the correct way - send all investment logic inside onSuccess block uploadFileTask

Since these QuickBlox requests are asynchronous

+2


source







All Articles