This article is on the basis of the previous blog post on the Android XMPP-based Instant Messaging 1-basic conversation, adding new features to File transfer
1. Initialization of file Transfer management class
Public StaticFiletransfermanager Getfiletransfermanager () {if(FileManager = =NULL) {Servicediscoverymanager Sdmanager=Servicediscoverymanager. Getinstancefor (connection); if(Sdmanager = =NULL) {Sdmanager=NewServicediscoverymanager (connection); } sdmanager.addfeature ("Http://jabber.org/protocol/disco#info"); Sdmanager.addfeature ("jabber:iq:privacy"); Filetransfernegotiator.setserviceenabled (Connection,true); FileManager=NewFiletransfermanager (connection); } returnFileManager;}
2, listen to receive files (file path created good, otherwise will error), directly receive files, no write confirmation
Private voidAddfilelisterer () {Filetransfermanager Manager=Xmppconnection.getfiletransfermanager (); Manager.addfiletransferlistener (NewFiletransferlistener () {@Override Public voidFiletransferrequest (Finalfiletransferrequest request) { NewThread () {@Override Public voidrun () {Incomingfiletransfer transfer=request.accept (); String FileName=Transfer.getfilename (); //out of the handler to show the message .string[] args =Newstring[] {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=NewFile (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. send file, send picture test here
Client file Send: Toid to add a string "/smack", send to the other platform to add the corresponding string;
Here the mobile client sends the toid+ "/spark" to the Spark client when sending the file
Private void GetImage () { new Intent (); Intent.settype ("image/*"); Intent.setaction (intent.action_get_content); Startactivityforresult (Intent, tupian_result);}
protected voidOnactivityresult (intRequestcode,intResultCode, 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); intindex =Cursor.getcolumnindexorthrow (MediaStore.Images.Media.DATA); Cursor.movetofirst (); String Img_path=cursor.getstring (index); Newsendfiletask (). Execute (Img_path, touserid);};
classSendfiletaskextendsAsynctask<string, Integer, integer> { protectedInteger doinbackground (String ... params) {if(Params.length < 2) { returnInteger.valueof (-1); } String Img_path= Params[0]; String toid= Params[1] + "/spark"; Filetransfermanager Filetransfermanager=Xmppconnection.getfiletransfermanager (); File Filetosend=NewFile (Img_path); if(filetosend.exists () = =false) { return-1; } //out of the handler to show the message .string[] args =Newstring[] {tousername, img_path}; Android.os.Message msg=Handler.obtainmessage (); Msg.what= 1; Msg.obj=args; Msg.sendtotarget (); Outgoingfiletransfer Transfer=Filetransfermanager. Createoutgoingfiletransfer (toid);//Create an output File Transfer object 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 (); } return0; }}
SOURCE Send: Http://files.cnblogs.com/files/pear-lemon/XmppTest.zip
Android XMPP-based Instant Messaging 2-File transfer