本文是在上一篇博文Android基于XMPP的即时通讯1-基本对话的基础上,添加新的功能,文件传输
1、初始化文件传输管理类
public static FileTransferManager getFileTransferManager() { if (fileManager == null) { ServiceDiscoveryManager sdManager = ServiceDiscoveryManager .getInstanceFor(connection); if (sdManager == null) { sdManager = new ServiceDiscoveryManager(connection); } sdManager.addFeature("http://jabber.org/protocol/disco#info"); sdManager.addFeature("jabber:iq:privacy"); FileTransferNegotiator.setServiceEnabled(connection, true); fileManager = new FileTransferManager(connection); } return fileManager; }
2、监听接收文件(文件路径创建好,不然会报错),直接接收文件,没有写确认
private void addFileListerer() { FileTransferManager manager = XmppConnection.getFileTransferManager(); manager.addFileTransferListener(new FileTransferListener() { @Override public void fileTransferRequest(final FileTransferRequest request) { new Thread() { @Override public void run() { IncomingFileTransfer transfer = request.accept(); String fileName = transfer.getFileName(); // 在handler里取出来显示消息 String[] args = new String[] { toUserName, fileName }; android.os.Message msg = handler.obtainMessage(); msg.what = 1; msg.obj = args; msg.sendToTarget(); String save_path = Environment .getExternalStorageDirectory() + File.separator + "XmppTest" + File.separator + fileName; File file = new File(save_path); try { transfer.recieveFile(file); while (!transfer.isDone()) { if (transfer.getStatus().equals(FileTransfer.Status.error)) { System.out.println("ERROR!!! " + transfer.getError()); } else { } try { Thread.sleep(1000L); } catch (Exception e) { } } } catch (XMPPException e) { e.printStackTrace(); } }; }.start(); } }); }
3、发送文件,这里发送图片测试
客户端文件发送:toId后边要加一个字符串"/Smack",发送给其他的平台要加相应的字符串;
这里手机客户端发送给Spark客户端,发送文件时toId+"/Spark"
private void getImage(){ Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(intent, TUPIAN_RESULT); }
protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); Uri uri = data.getData(); String[] proj = { MediaStore.Images.Media.DATA }; Cursor cursor = activity.managedQuery(uri, proj, null, null, null); int index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); String img_path = cursor.getString(index); new SendFileTask().execute(img_path, toUserID); };
class SendFileTask extends AsyncTask<String, Integer, Integer> { protected Integer doInBackground(String... params) { if (params.length < 2) { return Integer.valueOf(-1); } String img_path = params[0]; String toId = params[1] + "/Spark"; FileTransferManager fileTransferManager = XmppConnection.getFileTransferManager(); File filetosend = new File(img_path); if (filetosend.exists() == false) { return -1; } // 在handler里取出来显示消息 String[] args = new String[] { toUserName, img_path }; android.os.Message msg = handler.obtainMessage(); msg.what = 1; msg.obj = args; msg.sendToTarget(); OutgoingFileTransfer transfer = fileTransferManager .createOutgoingFileTransfer(toId);// 创建一个输出文件传输对象 try { transfer.sendFile(filetosend, "recv img"); while (!transfer.isDone()) { if (transfer.getStatus().equals(FileTransfer.Status.error)) { System.out.println("ERROR!!! " + transfer.getError()); } else { } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } catch (XMPPException e1) { e1.printStackTrace(); } return 0; } }