5

我做了一个聊天应用程序。我想在我的应用程序中添加照片/文件共享概念,与 WhatsApp 相同。我已经使用当前应用程序制作了应用程序Xmpp/Openfire,现在我正在使用此功能进行照片共享,但这并不完全可靠:

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();

    }

我想像 WhatsApp 或任何其他好方法一样做。请提出一个好的解决方案

谢谢

4

0 回答 0