我如何在聊天应用程序中将文件发送给我的其他好友?

时间:2015-01-15 07:19:52

标签: android iphone sockets google-app-engine xmpp

我有一个聊天应用程序,我想添加将文件发送给我的伙伴的功能,我的伙伴也发送给我文件。我怎么能这样做?谢谢!我的好友在其他城市

1 个答案:

答案 0 :(得分:2)

尝试使用此代码通过xmpp向其他用户发送数据,并确保用户必须是您的朋友和在线,否则您将无法发送数据。

private void sendData(File file) {


        Log.i("Sending1","sending1");

        try {
            progressDialog = ProgressDialog.show(ChatActivity.this.getParent(),"", "Please wait.");
            OutgoingFileTransfer transfer = manager.createOutgoingFileTransfer(secondUserName+"/Smack");
            //  String name = "DemoImage";n
            Log.i("Sending2","sending2");

            transfer.sendFile(file, "Demo Image");

            while (!transfer.isDone()) {
                if (transfer.getStatus().equals(Status.error)) {
                    if(progressDialog != null && progressDialog.isShowing()){
                        progressDialog.cancel();
                    }
                    System.out.println("ERROR!!! " + transfer.getError());
                } else {
                    System.out.println(transfer.getStatus());
                    System.out.println(transfer.getProgress());
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(ChatActivity.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if(transfer.isDone()){
                if(progressDialog != null && progressDialog.isShowing()){
                    progressDialog.cancel();
                }
                if(transfer.getStatus().equals(Status.error))
                {

                    Constant.showToast(ChatActivity.this.getParent(), "Error in sending file please retry.");
                }else{
// do what you want.
}


            }
            //transfer.sendFile(fi, "Demo Images");
            Log.i("Sending3","sending3");
            //  transfer.sendFile(new File(filePath), "You won't believe this!");  
            //transfer.sendFile(new File("shakespeare_complete_works.txt"), "You won't believe this!");
        } catch (XMPPException e) {
            // TODO Auto-generated catch block
            if(progressDialog != null && progressDialog.isShowing()){
                progressDialog.cancel();
            }
            e.printStackTrace();
            Log.i("Sending4","sending4");
        } catch (Exception e) {
            // TODO: handle exception
            if(progressDialog != null && progressDialog.isShowing()){
                progressDialog.cancel();
            }
            e.printStackTrace();
        } 

    }

用于文件传输配置

 private void configFileTransfer() {

            ProviderManager.getInstance().addIQProvider("query","http://jabber.org/protocol/bytestreams", new BytestreamsProvider());
            ProviderManager.getInstance().addIQProvider("query","http://jabber.org/protocol/disco#items", new DiscoverItemsProvider());
            ProviderManager.getInstance().addIQProvider("query","http://jabber.org/protocol/disco#info", new DiscoverInfoProvider());

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

        }

private void connect() {
        if(XMPPSmackConnection.connection==null || !XMPPSmackConnection.connection.isConnected()){ 
            reConnect();
        }else{
            XMPPSmackConnection.getInstance();
            //request = new MessageEventManager(XMPPSmackConnection.connection);
            Presence presence = new Presence(Presence.Type.available);
            XMPPSmackConnection.connection.sendPacket(presence);
            setConnection(XMPPSmackConnection.getInstance().connection);
            mHandler= new Handler();
            configFileTransfer();
        }
    }

并在活动的oncreate上调用connect