I am trying to send an image file using smack and openfire xmpp. For this I am using FileTransferManager class. To use FileTransferManager class I am using asmack-android-6.jar. I followed this link to do file sharing. This issue is also shared in comments below on this tutorial but no good resolution is given to this issue. Then I searched over stack overflow, Many Developers have asked this question but only 1-2 have got replies that they have accepted, others not.
I studied all the answers that I found, tried all the ways that google gave me but still unable to solve this problem.
The code I used is:
d.findViewById(R.id.btnsendphoto).setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (!filepath.equals("")) {
ServiceDiscoveryManager sdm = ServiceDiscoveryManager
.getInstanceFor(connection);
if (sdm == null) {
sdm = new ServiceDiscoveryManager(
connection);
Log.e("service discovery", "SDM");
sdm.addFeature("http://jabber.org/protocol/disco#info");
sdm.addFeature("jabber:iq:privacy");
}
mFileTransferManager = new FileTransferManager(
connection);
/*
* OutgoingFileTransfer transfer =
* mFileTransferManager
* .createOutgoingFileTransfer
* ("98c6d889473a6fae@pc/Smack");
*/
String to = connection.getRoster()
.getPresence("98c6d889473a6fae@pc")
.getFrom();
OutgoingFileTransfer transfer = mFileTransferManager
.createOutgoingFileTransfer(to);
File file = new File(filepath);
try {
//[configureProviderManager](http://paste.ubuntu.com/9932239/)
configureProviderManager(connection);
transfer.sendFile(file, "test_file");
} catch (XMPPException e) {
e.printStackTrace();
}
while(!transfer.isDone()) {
Log.d("status", transfer.getStatus().toString());
Log.d("percent", new Long(transfer.getBytesSent()).toString());
if (transfer.getStatus() == Status.error) {
Log.e("percent", "Error " + new Long(transfer.getBytesSent()).toString() + " " + transfer.getError() + " " + transfer.getException());
transfer.cancel();
}
if(transfer.getStatus().equals(Status.refused))
System.out.println("refused " + transfer.getError());
else if( transfer.getStatus().equals(Status.error))
System.out.println(" error " + transfer.getError());
else if(transfer.getStatus().equals(Status.cancelled))
System.out.println(" cancelled " + transfer.getError());
else
System.out.println("Success");
}
}
d.dismiss();
}
});
The logcat I got is very big, so I gave link of that. So can anyone tell what mistake I am making or can suggest what amendment I make to achieve task
This problem got solved using this link answer don't know why its downvoted. Lemme share answer here also
d.findViewById(R.id.btnsendphoto).setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (!filepath.equals("")) {
ServiceDiscoveryManager sdm = ServiceDiscoveryManager
.getInstanceFor(connection);
if (sdm == null) {
sdm = new ServiceDiscoveryManager(
connection);
Log.e("service discovery", "SDM");
sdm.addFeature("http://jabber.org/protocol/disco#info");
sdm.addFeature("jabber:iq:privacy");
}
configureProviderManager(connection);
FileTransferNegotiator.IBB_ONLY = true;
FileTransferNegotiator.setServiceEnabled(connection, true);
mFileTransferManager = new FileTransferManager(
connection);
/*
* OutgoingFileTransfer transfer =
* mFileTransferManager
* .createOutgoingFileTransfer
* ("98c6d889473a6fae@pc/Smack");
*/
String to = connection.getRoster()
.getPresence("98c6d889473a6fae@pc")
.getFrom();
final OutgoingFileTransfer transfer = mFileTransferManager
.createOutgoingFileTransfer(to);
File file = new File(filepath);
try {
configureProviderManager(connection);
transfer.sendFile(file, "test_file");
} catch (XMPPException e) {
e.printStackTrace();
}
new AsyncTask<Void, Void, Void>() {
protected void onPreExecute() {
}
@Override
protected Void doInBackground(Void... params) {
while (!transfer.isDone()) {
if (transfer.getStatus().equals("Error")) {
Log.d("file transfer",
"ERROR!!! " + transfer.getError());
} else if (transfer.getStatus().equals("Cancelled")
|| transfer.getStatus().equals("Refused")) {
Log.d("file transfer",
"Cancelled!!! " + transfer.getError());
}
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return null;
};
protected void onPostExecute(Void result) {
if (transfer.getStatus().equals("Refused")
|| transfer.getStatus().equals("Error")
|| transfer.getStatus().equals("Cancelled")) {
Log.i("file transfer", "refused cancelled error "
+ transfer.getError());
} else {
Log.i("file transfer", "Success: " + transfer.getFileName());
}
};
}.execute();
}
d.dismiss();
}
});