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 may try this link: https://www.javacodegeeks.com/2012/08/android-file-transfer-with-asmack-and.html
For File send
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');
}
File Receive:
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();
}
});
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();
}
}
});
}