Sending Pictures like WhatsApp

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-06 04:36:10

问题


I have made a chatting application. I want to add photo/file sharing concept in my application same as WhatsApp. I have made the app using Xmpp/Openfire and current now I am using this function for photo sharing, but it is not all reliable :

public void sendFile(final String path, final String receiver) {

        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");
                sdm.addFeature("jabber:iq:privacy");

                // Create the file transfer manager
                FileTransferManager manager = new FileTransferManager(
                        connection);
                FileTransferNegotiator.setServiceEnabled(connection, true);

                // Create the outgoing file transfer
                OutgoingFileTransfer transfer = manager
                .createOutgoingFileTransfer(receiver + "/Smack");

                System.out.println("Receiver of the file is "+receiver+"/smack");


                Log.i("transfere file", "outgoingfiletransfer is created");

                try {

                    OutgoingFileTransfer.setResponseTimeout(30000);
                    transfer.sendFile(new File(path), "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) {
                                Log.i("transfere file", "Errorrr isss: "
                                        + transfer.getError());
                                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();

    }

I want to do it same as WhatsApp or any other good means. Please suggest a good solution

Thanks

来源:https://stackoverflow.com/questions/14601198/sending-pictures-like-whatsapp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!