سؤال

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?

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

You can try this:

public boolean sendFiles(XMPPConnection con,String fullJID,String filePath){  
    File files=new File(filePath);  
    FileTransferManager fileManager=new FileTransferManager(con);  
    OutgoingFileTransfer sendfile=fileManager.createOutgoingFileTransfer(fullJID);  
    try {  
        sendfile.sendFile(files, "Sending file");  
        return true;  
    } catch (XMPPException e) {  
        e.printStackTrace();  
        return false;  
    }  
}  

public void receiveFiles(XMPPConnection con,final String filePath){  
    FileTransferManager fileManager=new FileTransferManager(con);  
    fileManager.addFileTransferListener(new FileTransferListener() {  
        @Override  
        public void fileTransferRequest(FileTransferRequest prequest) {  
            //System.out.println("The file received from: " + prequest.getRequestor());  
            System.out.println("filePath:"+filePath+"||FileName:"+prequest.getFileName());  
            file = new File(filePath +"\\" +prequest.getFileName());  
            request = prequest;  
            IncomingFileTransfer infiletransfer =request.accept();  
            try {  
                infiletransfer.recieveFile(file);  
                System.out.println("success!");  
            } catch (XMPPException e) {  
                e.printStackTrace();  
            }  
        }  
    });  
}  
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top