使用Smack XMPP发送内嵌图像

时间:2014-03-08 16:31:18

标签: java xmpp smack instant-messaging

我即将创建一个简单的xmpp java客户端应用程序,为此我正在考虑使用Smack XMPP库,它运行良好。

客户端的一个功能应该是将内嵌图像发送给您的聊天室。 我浏览了SMACK的javadoc但我无法找到如何使用SMACK / XMPP发送图像或一般二进制数据。我不是在谈论接收用户必须接受的标准文件传输,而是在消息中发送的图像。 SMACK / XMPP可以实现吗?任何人都可以提供一个例子吗?

3 个答案:

答案 0 :(得分:1)

您可以通过使用smth来默认接受(用户不会被提示):

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

答案 1 :(得分:0)

您可以尝试以下链接:https://www.javacodegeeks.com/2012/08/android-file-transfer-with-asmack-and.html

对于文件发送

FileTransferManager manager = new FileTransferManager(connection);
OutgoingFileTransfer transfer = manager.createOutgoingFileTransfer('usre2@myHost/Smack');
File file = new File(filenameWithPath);
try {
   transfer.sendFile(file, 'test_file');
     } catch (XMPPException e) {
       e.printStackTrace();
}
while(!transfer.isDone()) {
   if(transfer.getStatus().equals(Status.error)) {
      System.out.println('ERROR!!! ' + transfer.getError());
   } else if (transfer.getStatus().equals(Status.cancelled)
                    || transfer.getStatus().equals(Status.refused)) {
      System.out.println('Cancelled!!! ' + transfer.getError());
   }
   try {
      Thread.sleep(1000L);
   } catch (InterruptedException e) {
      e.printStackTrace();
   }
}
if(transfer.getStatus().equals(Status.refused) || transfer.getStatus().equals(Status.error)
 || transfer.getStatus().equals(Status.cancelled)){
   System.out.println('refused cancelled error ' + transfer.getError());
} else {
   System.out.println('Success');
}

文件接收:

FileTransferManager manager = new FileTransferManager(connection);
manager.addFileTransferListener(new FileTransferListener() {
   public void fileTransferRequest(final FileTransferRequest request) {
      new Thread(){
         @Override
         public void run() {
            IncomingFileTransfer transfer = request.accept();
            File mf = Environment.getExternalStorageDirectory();
            File file = new File(mf.getAbsoluteFile()+'/DCIM/Camera/' + transfer.getFileName());
            try{
                transfer.recieveFile(file);
                while(!transfer.isDone()) {
                   try{
                      Thread.sleep(1000L);
                   }catch (Exception e) {
                      Log.e('', e.getMessage());
                   }
                   if(transfer.getStatus().equals(Status.error)) {
                      Log.e('ERROR!!! ', transfer.getError() + '');
                   }
                   if(transfer.getException() != null) {
                      transfer.getException().printStackTrace();
                   }
                }
             }catch (Exception e) {
                Log.e('', e.getMessage());
            }
         };
       }.start();
    }
 });

答案 2 :(得分:0)

您可以尝试以下方法:

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