如何在我的聊天应用程序上向我的其他好友发送文件?
Posted
技术标签:
【中文标题】如何在我的聊天应用程序上向我的其他好友发送文件?【英文标题】:How could I send file to my other buddy on my chat app? 【发布时间】:2015-01-15 07:19:51 【问题描述】:我有一个聊天应用程序,我想添加向我的好友发送文件的功能,我的好友也向我发送文件。我怎么能这样做?谢谢!我的朋友在其他城市
【问题讨论】:
【参考方案1】:尝试使用此代码通过 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
【讨论】:
以上是关于如何在我的聊天应用程序上向我的其他好友发送文件?的主要内容,如果未能解决你的问题,请参考以下文章
是否有事件或其他方式来处理用户在 Facebook Messenger 平台上向机器人发送的第一条消息?