如何在聊天时使用xmpp在设备之间发送文件

时间:2015-04-27 07:27:33

标签: android asmack

感谢您关注这个问题......

我在ejabbered和asmack_4.1库的帮助下制作了一个XMPP聊天应用程序。

我已经安装了一个用于Windows的PSI客户端与Android设备聊天。 我能够在设备之间进行文本聊天。 我也可以将文件从设备发送到PSI客户端和PSI客户端到Android设备。 但问题是将文件设备发送到设备。

我使用下面的代码来发送和接收文件:

文件发送:

public void fileTransfer(String filenameWithPath, Bitmap thumbnail, String userId) {
        // FileTransferManager manager = new FileTransferManager(connection);
        FileTransferManager manager = FileTransferManager.getInstanceFor(connection);
        //    OutgoingFileTransfer transfer = manager.createOutgoingFileTransfer("usre2@myHost/Smack");
        OutgoingFileTransfer transfer = manager.createOutgoingFileTransfer(userId + "/Admin"); // "/Good" depends on server
//        OutgoingFileTransfer transfer = manager.createOutgoingFileTransfer(userId + "/Directors-iMac");

        File file = new File(filenameWithPath);
        try {
            transfer.sendFile(file, "test_file");
        } catch (SmackException e) {
            e.printStackTrace();
        }

        while (!transfer.isDone()) {
            if (transfer.getStatus().equals(FileTransfer.Status.error)) {
                System.out.println("ERROR!!! " + transfer.getError());
            } else if (transfer.getStatus().equals(FileTransfer.Status.cancelled)
                    || transfer.getStatus().equals(FileTransfer.Status.refused)) {
                System.out.println("Cancelled!!! " + transfer.getError());
            }
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        if (transfer.getStatus().equals(FileTransfer.Status.refused) || transfer.getStatus().equals(FileTransfer.Status.error)
                || transfer.getStatus().equals(FileTransfer.Status.cancelled)) {
            System.out.println("refused cancelled error " + transfer.getError());
        } else {
            System.out.println("Success");
        }
    }

文件接收:

public void fileReciever(AbstractXMPPConnection connection) {
        //**************************************

        // Create the file transfer manager
        final FileTransferManager managerListner = FileTransferManager.getInstanceFor(connection);

        //    FileTransferNegotiator.setServiceEnabled(connection, true);
        FileTransferNegotiator negotiator = FileTransferNegotiator.getInstanceFor(connection);

        Log.i("File transfere manager", "created");

        // Create the listener
        managerListner
                .addFileTransferListener(new FileTransferListener() {
                    @Override
                    public void fileTransferRequest(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)) {

                                Log.i(RECIEVE_FILE_ALERT_DIALOG, "still receiving : " + (transfer.getProgress()) + " status " + transfer.getStatus());

                                if (transfer.getStatus().equals(FileTransfer.Status.error)) {
                                    // Log.i("Error file",
                                    // transfer.getError().getMessage());
                                    Log.i("Recieve File",
                                            "cancelling still receiving : "
                                                    + (transfer.getProgress())
                                                    + " status "
                                                    + transfer.getStatus());
                                    transfer.cancel();

                                    break;
                                }
                            }

                        } /*catch (XMPPException e) {

                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }*/ catch (SmackException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    }
                });

        //**************************************
    }

注意:在fileTransfer()方法中,我使用的是以下行:

OutgoingFileTransfer transfer = manager.createOutgoingFileTransfer(userId + "/Admin"); // "/Good" depends on server

我可以将文件从Android设备发送到PSI桌面应用程序,反之亦然,但我无法在Android设备之间传输文件。

0 个答案:

没有答案