javaxmppsmackinstant-messaging

Sending inline images using Smack XMPP


I am about to create a simple xmpp java client application and for this I am considering to use the Smack XMPP library, which works pretty well.

One feature of the client should be to send inline images to your chatpartner. I browsed through the javadoc of SMACK but I was not able to find how it's possible to send images or in general binary data with SMACK/XMPP. I am not talking about a standard file transfer that the receiving user has to accept, but rather an image which can be sent within a message. Is this possible with SMACK/XMPP? Can anybody provide an example?


Solution

  • You can just make it accepted by default (the user won't be prompted) by using smth like:

    final FileTransferManager manager = new FileTransferManager(connection); //Use your xmpp connection
    manager.addFileTransferListener(new FileTransferListener() {
        public void fileTransferRequest(FileTransferRequest request) {
                IncomingFileTransfer transfer = request.accept();
                try {
                    InputStream input = transfer.recieveFile();
                    //This will be a binary stream and you can process it. Create image and display it inline in your chat app.
                } catch (XMPPException e) {
                    e.printStackTrace();
                }
            }
        }