4

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

Community
  • 1
  • 1
Android Rockss
  • 215
  • 3
  • 13
  • I'd like to help, but all I can do is to point at https://github.com/igniterealtime/Smack/wiki/Smack-XMPP-File-Transfer – Flow Jan 29 '15 at 09:43
  • ok I study the link you gave, thanks for commenting – Android Rockss Jan 29 '15 at 09:53
  • @Flow I read the link you gave but i haven't found solution for my problem there – Android Rockss Jan 29 '15 at 10:46
  • The content of the linked page basically tries to tell you that you should be able to solve the problem yourself. Because since Smack is open source and XMPP is an open standard you can debug and analyze the cause for the failing file transfer yourself. – Flow Jan 29 '15 at 11:02
  • i am not able to reply this comment, but still thanks, you gave your precious time – Android Rockss Jan 29 '15 at 11:06

2 Answers2

1

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();
                        }
                    });
Community
  • 1
  • 1
Android Rockss
  • 215
  • 3
  • 13
0

I had same problem, I investigated the stanza and solved it this way.

Many people use "/Smack" or "/Resource" as resource part in jid, but it can be configured also the another way.

Resource path is changing with every presence changed of user. Lets say we want to send image to this user: "user1@mydomain"

You must add "/Resource" part to this jid and it become this: user1@mydomain/Resource

But /Resource path is changing with presence so you must follow every presence change to update resource path. Best way is to get user presence is in roster listener and in presencheChanged() method you get last user resource part like this:

Roster roster=getRoster();
roster.addRosterListener(new RosterListener() {
                @Override
                public void entriesAdded(Collection<Jid> addresses) {
                    Log.d("entriesAdded", "ug");
                    context.sendBroadcast(new Intent("ENTRIES_ADDED"));
                }

                @Override
                public void entriesUpdated(Collection<Jid> addresses) {
                    Log.d("entriesUpdated", "ug");
                }

                @Override
                public void entriesDeleted(Collection<Jid> addresses) {
                    Log.d("entriesDeleted", "ug");
                }

                @Override
                public void presenceChanged(Presence presence) {
                    Log.d("presenceChanged", "ug");
                    //Resource from presence
                    String resource = presence.getFrom().getResourceOrEmpty().toString();
                    //Update resource part for user in DB or preferences
                    //...
                }
            });
}

Resource string will be some generated string like "6u1613j3kv" and jid will become:

user1@mydomain/6u1613j3kv

That means that you must create your outgoing transfer like this:

EntityFullJid jid = JidCreate.entityFullFrom("user1@mydomain/6u1613j3kv"); 
OutgoingFileTransfer transfer = manager.createOutgoingFileTransfer(jid)
transfer.sendFile(new File("DirectoryPath"), "Description");

And that is how i have solved my problem with file transfer on smack and Openfire.

In your case form jid like this:

String to = connection.getRoster().getPresence("98c6d889473a6fae@pc").getFrom();
String Resource = connection.getRoster().getPresence("98c6d889473a6fae@pc").getFrom().getResourceOrEmpty().toString();
OutgoingFileTransfer transfer = mFileTransferManager.createOutgoingFileTransfer(to + "/" + resource);

Also to mention you must add following properties in your Openfire server:

xmpp.proxy.enabled - true
xmpp.proxy.externalip - MY_IP_ADDRESS
xmpp.proxy.port - 7777

Just to mention, I am using Openfire 4.0.2 and Smack 4.2.2.

Also this can be configured the easy way, just set the resource on

XMPPTCPConnectionConfiguration.Builder .

like

XMPPTCPConnectionConfiguration.Builder configurationBuilder = 
XMPPTCPConnectionConfiguration.builder(); 

configurationBuilder.setResource("yourResourceName");
Kenan Begić
  • 1,228
  • 11
  • 21