2013-04-26 55 views
1

我是Android編程的新手。我正在嘗試開發具有文件傳輸功能的Messenger應用程序。但有困難重新文件傳輸。Android XMPP文件傳輸

viewer.java**

Button transfer = (Button) findViewById(R.id.btnimage);  
transfer.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent(); 
       intent.setType("image/*"); 
       intent.setAction(Intent.ACTION_GET_CONTENT); 
       startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);     
      } 
     }); 

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
      if (resultCode == RESULT_OK) { 
       if (requestCode == SELECT_PICTURE) { 
        Uri selectedImageUri = data.getData(); 
        selectedImagePath = getPath(selectedImageUri); 
        System.out.println("Image Path : " + selectedImagePath); 
        XMPPClient.getInstance().SendFile(to, selectedImagePath); 
       } 
      } 
     } 

     public String getPath(Uri uri) { 
      String[] projection = { MediaStore.Images.Media.DATA }; 
      Cursor cursor = managedQuery(uri, projection, null, null, null); 
      int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
      cursor.moveToFirst(); 
      return cursor.getString(column_index); 
     } 

,然後我有XMPPCLIENT.java

public void ReceiveFile() { 
     Thread thread = new Thread() { 
      public void run() { 
       ServiceDiscoveryManager sdm = ServiceDiscoveryManager 
         .getInstanceFor(getConnection()); 

       if (sdm == null) 
        sdm = new ServiceDiscoveryManager(getConnection()); 

       sdm.addFeature("http://jabber.org/protocol/disco#info"); 

       sdm.addFeature("jabber:iq:privacy"); 

       // Create the file transfer manager 
       final FileTransferManager managerListner = new FileTransferManager(
         getConnection()); 

       FileTransferNegotiator.setServiceEnabled(getConnection(), true); 

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

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

             Thread.sleep(1000); 
             Log.i("Recieve File alert dialog", 
               "still receiving : " 
                 + (transfer 
                   .getProgress()) 
                 + " status " 
                 + transfer.getStatus()); 

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

              break; 
             } 

            } 

           } catch (XMPPException e) { 

            // TODO Auto-generated catch block 
            e.printStackTrace(); 
           } catch (InterruptedException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
           } 
          } 
         }); 

      } 
     }; 
     thread.start(); 
    } 

    public void SendFile(final String Receiver, final String Directory) { 
     Thread thread = new Thread() { 
      public void run() { 

      ServiceDiscoveryManager sdm = ServiceDiscoveryManager 
      .getInstanceFor(getConnection()); 
      if (sdm == null) 
       sdm = new ServiceDiscoveryManager(getConnection()); 
      sdm.addFeature("http://jabber.org/protocol/disco#info"); 
      sdm.addFeature("jabber:iq:privacy"); 
      // Create the file transfer manager 
      FileTransferManager manager = new FileTransferManager(
        mConnection); 
      FileTransferNegotiator 
      .setServiceEnabled(getConnection(), true); 

      // Create the outgoing file transfer 
      OutgoingFileTransfer transfer = manager 
      .createOutgoingFileTransfer(getJIDofUserID(Receiver)); 
      sendMessage(getJIDofUserID(Receiver), "image"); 
      Log.i("transfere file", "outgoingfiletransfere is created"); 
      try { 
       OutgoingFileTransfer.setResponseTimeout(30000); 
       transfer.sendFile(new File(Directory), "Description"); 
       Log.i("transfere file", "sending file"); 
       while (!transfer.isDone()) { 
        try { 
         Thread.sleep(1000); 
         Log.i("transfere file", "sending file status " 
           + transfer.getStatus() + "progress: " 
           + transfer.getProgress()); 
         if (transfer.getStatus() == org.jivesoftware.smackx.filetransfer.FileTransfer.Status.error) { 
          transfer.cancel(); 
          Log.e("",transfer.getError()+" error"); 
          break; 
         } 
        } catch (InterruptedException e) { 
         // TODO Auto-generated catch block 
         Log.e("aaaaaaaaaaaaaaa","aaaa"+e); 
         e.printStackTrace(); 
        } 
       } 
      } 
      catch (XMPPException e) { 
       // TODO Auto-generated catch block 
       Log.e("aaaaaaaaaaaaaaa","aaaa"+e); 
       e.printStackTrace(); 
      } 
      Log.i("transfere file", "sending file done"); 
     } 
     }; 
     thread.start(); 
    } 

所以我的問題是:

  1. 是連接,我使用的是相同的連接聊天(默認端口:5222)?
  2. 當我觸發sendfile(final String Receiver, final String Directory)函數時,接收者何時以及如何得到任何通知?
+0

你解決了嗎? – 2014-04-21 08:56:57

+0

新的FileTransferManager(連接); 在這個參數中需要什麼連接,,, xmppConnection默認或具有某些屬性??? – 2014-08-06 11:24:19

回答

0

回答您的問題:

  1. 這取決於它用於文件傳輸方法。 XMPP知道幾個。您可以在這裏閱讀更多關於它的信息https://github.com/igniterealtime/Smack/wiki/Smack-XMPP-File-Transferhttp://xmpp.org/xmpp-protocols/xmpp-extensions/

  2. 接收實體必須首先確認文件傳輸。但是,這取決於其他客戶如何實現,例如如果有彈出窗口說有傳入文件傳輸。另一方面的客戶端也可能被配置爲自動接受所有請求。

0

首先將以下配置添加到您的xmpp配置類中。

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

然後在openfire服務器中添加或編輯以下三個屬性。

  1. xmpp.proxy.enabled - 真
  2. xmpp.proxy.port - 7777
  3. xmpp.proxy.externalip - [公開訪問的主機或IP]

請這些變化,並再試一次 。