powered by Jive Software

Android file transfer using smack

I used the davanum example and chat is working fine on the android emulator. I noticed the discussion surrounding the “android smack” jar is pretty old. So I tried using the latest smack.jar and smackx.jar… and chat still works fine in the emulator.

I used another example from here about smack file transfer (not on android), and that works (using latest smack.jar and smackx.jar).

But when I try the file transfer code in the android emulator, it doesn’t work. It fails on this “FileTransferNegotiator.setServiceEnabled(connection,true);” with a null pointer exception.

I noticed some discussion surroudning this on here, they are months old, so I’m not sure how relevant they are now. In any case, I haven’t found a solution yet.

Please help! Any help is greatly appreciated.

Anyone? I still haven’t gotten it to work…

// xmppConn --> your XMPPConnection

xmppConn.login(userId,password,resouce );

// set the serviceDiscoveryManager

ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(xmppConn);

if (sdm == null)

sdm = new ServiceDiscoveryManager(xmppConn);

    sdm.addFeature("[http://jabber.org/protocol/disco#info](http://jabber.org/protocol/disco#info)");

sdm.addFeature(“jabber:iq:privacy”);

good lucks

I am able to send an receive files from and to asmack via bytestreams (couldn’t test ibb yet). Regarding sending files from asmack: There seems to be a little bug in smack, which prevented me sending files from asmack (receiving was fine). See also my other post: http://community.igniterealtime.org/thread/44145?tstart=0

You can get the asmack lib including the fix that I use from my clone of the main project: http://code.google.com/r/fschmaus-gtalksms/source/browse#hg%2Flib

Code examples are also there.

Regarding your NPE: Check out

configure(ProviderManager pm) in http://code.google.com/r/fschmaus-gtalksms/source/browse/src/com/googlecode/gtal ksms/XmppManager.java , maybe you forgot it.

Flow

hey flow,

can u help me with some code snippets for file trasfer over xmpp.As i have tried gtalk sms but it gives me errors for some variables and classes though i have added all four libs.

GTalkSMS is open source and has the ability to send and receive files => You will find code snippets there.

Hi,

I have download the code for gtalk sms from code.google and run it device.but i m getting only friendlist and not able to do anything.like when i click on friend list one dialog is opening that shows name and status of friend.

so i m confused how can i send file with this code.can u suggest me the way?

Read the documenation and/or the frontpage of the project site. GtalkSMS is **not **a chat client. (But thats a command mistake, although it’s stated everywhere ) Everything you need is in the wiki of the project.

Hi flow,

as i have gone through the wiki and code also.there is one file FileCmd.java.I guess file transfer must be done through that class.do u give me some more idea for the same?

is there anyone who can help me out?

hi hitendrasinh, i am also facing problem in file sending from my android application. did you resolve this problem.

please help me any one, to find out the exact solution for this issue.

first u need to add this method and call it before connecting to the server

“”""""

“”"""

public void configure(ProviderManager pm) {

// Private Data Storage

pm.addIQProvider(“query”, “jabber:iq:private”,

new PrivateDataManager.PrivateDataIQProvider());

// Time

try {

pm.addIQProvider(“query”, “jabber:iq:time”,

Class.forName(“org.jivesoftware.smackx.packet.Time”));

} catch (ClassNotFoundException e) {

Log.w(“TestClient”,

“Can’t load class for org.jivesoftware.smackx.packet.Time”);

}

// Roster Exchange

pm.addExtensionProvider(“x”, “jabber:x:roster”,

new RosterExchangeProvider());

// Message Events

pm.addExtensionProvider(“x”, “jabber:x:event”,

new MessageEventProvider());

// Chat State

pm.addExtensionProvider(“active”,

            "[http://jabber.org/protocol/chatstates](http://jabber.org/protocol/chatstates)",

new ChatStateExtension.Provider());

pm.addExtensionProvider(“composing”,

            "[http://jabber.org/protocol/chatstates](http://jabber.org/protocol/chatstates)",

new ChatStateExtension.Provider());

pm.addExtensionProvider(“paused”,

            "[http://jabber.org/protocol/chatstates](http://jabber.org/protocol/chatstates)",

new ChatStateExtension.Provider());

pm.addExtensionProvider(“inactive”,

            "[http://jabber.org/protocol/chatstates](http://jabber.org/protocol/chatstates)",

new ChatStateExtension.Provider());

pm.addExtensionProvider(“gone”,

            "[http://jabber.org/protocol/chatstates](http://jabber.org/protocol/chatstates)",

new ChatStateExtension.Provider());

// XHTML

    pm.addExtensionProvider("html", "[http://jabber.org/protocol/xhtml-im](http://jabber.org/protocol/xhtml-im)",

new XHTMLExtensionProvider());

// Group Chat Invitations

pm.addExtensionProvider(“x”, “jabber:x:conference”,

new GroupChatInvitation.Provider());

// Service Discovery # Items

    pm.addIQProvider("query", "[http://jabber.org/protocol/disco#items](http://jabber.org/protocol/disco#items)",

new DiscoverItemsProvider());

// Service Discovery # Info

    pm.addIQProvider("query", "[http://jabber.org/protocol/disco#info](http://jabber.org/protocol/disco#info)",

new DiscoverInfoProvider());

// Data Forms

pm.addExtensionProvider(“x”, “jabber:x:data”, new DataFormProvider());

// MUC User

    pm.addExtensionProvider("x", "[http://jabber.org/protocol/muc#user](http://jabber.org/protocol/muc#user)",

new MUCUserProvider());

// MUC Admin

    pm.addIQProvider("query", "[http://jabber.org/protocol/muc#admin](http://jabber.org/protocol/muc#admin)",

new MUCAdminProvider());

// MUC Owner

    pm.addIQProvider("query", "[http://jabber.org/protocol/muc#owner](http://jabber.org/protocol/muc#owner)",

new MUCOwnerProvider());

// Delayed Delivery

pm.addExtensionProvider(“x”, “jabber:x:delay”,

new DelayInformationProvider());

// Version

try {

pm.addIQProvider(“query”, “jabber:iq:version”,

Class.forName(“org.jivesoftware.smackx.packet.Version”));

} catch (ClassNotFoundException e) {

// Not sure what’s happening here.

}

// VCard

pm.addIQProvider(“vCard”, “vcard-temp”, new VCardProvider());

// Offline Message Requests

    pm.addIQProvider("offline", "[http://jabber.org/protocol/offline](http://jabber.org/protocol/offline)",

new OfflineMessageRequest.Provider());

// Offline Message Indicator

pm.addExtensionProvider(“offline”,

            "[http://jabber.org/protocol/offline](http://jabber.org/protocol/offline)",

new OfflineMessageInfo.Provider());

// Last Activity

pm.addIQProvider(“query”, “jabber:iq:last”, new LastActivity.Provider());

// User Search

pm.addIQProvider(“query”, “jabber:iq:search”, new UserSearch.Provider());

// SharedGroupsInfo

pm.addIQProvider(“sharedgroup”,

            "[http://www.jivesoftware.org/protocol/sharedgroup](http://www.jivesoftware.org/protocol/sharedgroup)",

new SharedGroupsInfo.Provider());

// JEP-33: Extended Stanza Addressing

pm.addExtensionProvider(“addresses”,

            "[http://jabber.org/protocol/address](http://jabber.org/protocol/address)",

new MultipleAddressesProvider());

// FileTransfer

    pm.addIQProvider("si", "[http://jabber.org/protocol/si](http://jabber.org/protocol/si)",

new StreamInitiationProvider());

    pm.addIQProvider("query", "[http://jabber.org/protocol/bytestreams](http://jabber.org/protocol/bytestreams)",

new BytestreamsProvider());

    // pm.addIQProvider("open","[http://jabber.org/protocol/ibb](http://jabber.org/protocol/ibb)", new

// IBBProviders.Open());

//

    // pm.addIQProvider("close","[http://jabber.org/protocol/ibb](http://jabber.org/protocol/ibb)", new

// IBBProviders.Close());

//

    // pm.addExtensionProvider("data","[http://jabber.org/protocol/ibb](http://jabber.org/protocol/ibb)", new

// IBBProviders.Data());

//

// Privacy

pm.addIQProvider(“query”, “jabber:iq:privacy”, new PrivacyProvider());

    pm.addIQProvider("command", "[http://jabber.org/protocol/commands](http://jabber.org/protocol/commands)",

new AdHocCommandDataProvider());

pm.addExtensionProvider(“malformed-action”,

            "[http://jabber.org/protocol/commands](http://jabber.org/protocol/commands)",

new AdHocCommandDataProvider.MalformedActionError());

pm.addExtensionProvider(“bad-locale”,

            "[http://jabber.org/protocol/commands](http://jabber.org/protocol/commands)",

new AdHocCommandDataProvider.BadLocaleError());

pm.addExtensionProvider(“bad-payload”,

            "[http://jabber.org/protocol/commands](http://jabber.org/protocol/commands)",

new AdHocCommandDataProvider.BadPayloadError());

pm.addExtensionProvider(“bad-sessionid”,

            "[http://jabber.org/protocol/commands](http://jabber.org/protocol/commands)",

new AdHocCommandDataProvider.BadSessionIDError());

pm.addExtensionProvider(“session-expired”,

            "[http://jabber.org/protocol/commands](http://jabber.org/protocol/commands)",

new AdHocCommandDataProvider.SessionExpiredError());

}

then call it like this :

ConnectionConfiguration connConfig = new ConnectionConfiguration(

“server”, port, “”);

connConfig.setReconnectionAllowed(true);

connConfig.setDebuggerEnabled(true);

configure(ProviderManager.getInstance());

connection = new XMPPConnection(connConfig);

connection.connect();

for sending file:

“”""""""""""""

public void SendFile(final String Receiver, final String Directory) {

Thread thread = new Thread() {

public void run() {

ServiceDiscoveryManager sdm = ServiceDiscoveryManager

.getInstanceFor(connection);

if (sdm == null)

sdm = new ServiceDiscoveryManager(connection);

            sdm.addFeature("[http://jabber.org/protocol/disco#info](http://jabber.org/protocol/disco#info)");

sdm.addFeature(“jabber:iq:privacy”);

// Create the file transfer manager

FileTransferManager manager = new FileTransferManager(

connection);

FileTransferNegotiator

.setServiceEnabledconnection, true);

// Create the outgoing file transfer

OutgoingFileTransfer transfer = manager

.createOutgoingFileTransfer(Receiver

Log.i(“transfere file”, “outgoingfiletransfere is created”);

try {

OutgoingFileTransfer.setResponseTimeout(30000);

transfer.sendFile(new File(Directory), “Description”);

Log.i(“transfere file”, “sending file”);

while (!transfer.isDone()) {

try {

Thread.sleep(1000);

Log.i(“transfere file”, "sending file status "

  • transfer.getStatus() + "progress: "

  • transfer.getProgress());

if (transfer.getStatus() == Status.error) {

transfer.cancel();

break;

}

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

} catch (XMPPException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

Log.i(“transfere file”, “sending file done”);

}

};

thread.start();

}

1 Like

to receive a file use this method

“”""""""

“”"""""""""

public void ReceiveFile() {

Thread thread = new Thread() {

public void run() {

ServiceDiscoveryManager sdm = ServiceDiscoveryManager

.getInstanceFor(connection);

if (sdm == null)

sdm = new ServiceDiscoveryManager(connection);

            sdm.addFeature("[http://jabber.org/protocol/disco#info](http://jabber.org/protocol/disco#info)");

sdm.addFeature(“jabber:iq:privacy”);

// Create the file transfer manager

final FileTransferManager managerListner = new FileTransferManager(

connection);

FileTransferNegotiator

.setServiceEnabled(connection, true);

Log.i(“File transfere manager”, “created”);

// Create the listener

managerListner

.addFileTransferListener(new FileTransferListener() {

public void fileTransferRequest(

final FileTransferRequest request) {

Log.i(“Recieve File”,

“new file transfere request new file transfere request new file transfere request”);

Log.i(“file request”,

“from” + request.getRequestor());

IncomingFileTransfer transfer = request.accept();

Log.i(“Recieve File alert dialog”, “accepted”);

try {

transfer.recieveFile(new File("/sdcard/"

  • request.getFileName()));

while (!transfer.isDone() || (transfer.getProgress() < 1)) {

Thread.sleep(1000);

Log.i(“Recieve File alert dialog”, "still receiving : "

  • (transfer.getProgress()) + " status "

  • transfer.getStatus());

if (transfer.getStatus().equals(Status.error)) {

// Log.i(“Error file”,

// transfer.getError().getMessage());

Log.i(“Recieve File alert dialog”,

"cancelling still receiving : "

  • (transfer.getProgress())

  • " status "

  • transfer.getStatus());

transfer.cancel();

break;

}

}

} catch (XMPPException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

});

}

};

thread.start();

}

2 Likes

Hi Sherif,

I am getting the output like this

at the sender side-

0.0 is done!

Negotiating TransferProgress0.0

0.0 is done!

Negotiating StreamProgress0.0

0.0 is done!

Negotiating StreamProgress0.0

0.0 is done!

Negotiating StreamProgress0.0

0.0 is done!

Negotiating StreamProgress0.0

0.0 is done!

Negotiating StreamProgress0.0

0.0 is done!

Negotiating StreamProgress0.0

0.0 is done!

Negotiating StreamProgress0.0

0.0 is done!

Negotiating StreamProgress0.0

0.0 is done!

Negotiating StreamProgress0.0

and at receiver side i get the following output

addFileTransferListener method is called.

File Received4880.jpg1839

I am getting the message as file received but nothing is received. Have you checked this code on emulator? Or it works only on real devices?

Please help me on this as I am not able to figure out the exact problem.

Regards,

Shivprasad

Hi Shiva,

I am also struggling with same problem. I also tested with real device one is SAMSUNG Galaxy TABLET and HTC - Nexus One. If you found the solution can you let me know the fix or solution. If I find I will mail you or post the solution.

Here is my mail id madhuvemuri@gmail.com

Hi Madhu,

If you are using the above code as it is then check your SDCARD directory, the image received is saved in the parent SDCARD directory.

Let me know if you have any queries regarding XMPP chat app and related ones.

Thanks

~regards

Gaurav Arora

Hi Gaurav,

Iam trying to transfer the file based on above code. Always struck with the Negotiating Transfer progress 0.0 and it tries for 5 times same Negotiating state and next time moves to error state. Can you please help me out.

Thanks

Bala

Hi Gaurav,

I am developing an android XMPP chat app, When I call the MultiUserChat.getJoinedRooms(connection, user) method I am getting a NullPointer Exception. Do you know any fix?

Thanks,

Jashan

Yes I will help you out. Send me your code and I will help you out