Android file transfer using smack

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

what is the means of Receiver+"@domain.com"+"/Smack"?

i think its mean imran.khan@gmail.com/Smack ?

please any one update it.

thanks .

any one tell me where i will used this ReceiveFile() method for receive the file .

this will be used in oncreate() method or onresume() method .

please any one update .

thanks.

Hello mate, i have same probelm as you do and am Struggling there too have you fixed that problem?

hey sherif i am using your code for file receive and transfer
the Issue that i am facing is that when i am sendin a file from one client to another the other cliend is able to receive the file But the sender is not able to receive any file…
if i restart the application the sender is able to receive but once he sends a file again the same thing happen… i have posted the details error log and the code in This Links…

and also in

http://stackoverflow.com/questions/24775125/not-able-to-receive-file-if-i-send-a -file-first-with-asmack-and-openfire

please help me out with The issue…

I was able to use the code of Sherif and get file transfer working. It turned out to be an issue with whom the file is addressed to.

The Address needs to be of format : username@domain.com/resource_id

Avoid using /Smack if its already present. /Smack is the resource name.

In my case it was :

alokomkar@server_ip/mobile

Resource id may differ.