2012-12-27 63 views
2

我正在使用xmpp進行文件傳輸。這是我的文件傳輸代碼。使用xmpp在Android中傳輸文件

ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection); 
if (sdm == null) 
    sdm = new ServiceDiscoveryManager(connection); 
FileTransferManager manager = new FileTransferManager(connection); 
OutgoingFileTransfer transfer = manager.createOutgoingFileTransfer("[email protected]/Smack");   
File file = new File(filenameWithPath); 

try { 
    transfer.sendFile(file, "You won't believe this!"); 
    } catch (XMPPException e) { 
     e.printStackTrace(); 
    } 

while (!transfer.isDone()) { 
    Log.i("progres", "" + transfer.getProgress()); 
} 
if (transfer.getStatus().equals(Status.refused)|| transfer.getStatus().equals(Status.error) || transfer.getStatus().equals(Status.cancelled)) 
{  
    System.out.println("refused cancelled error " + transfer.getError()); 
} else 
{ 
    System.out.println("Success"); 
} 

問題是,當接受文件的時候它會崩潰。

日誌文件:

FATAL EXCEPTION: File Transfer jsi_1117022495316866568 
java.lang.ClassCastException: org.jivesoftware.smack.util.PacketParserUtils$2 
at org.jivesoftware.smackx.filetransfer.FileTransferNegotiator.negotiateOutgoingTransfer(FileTransferNegotiator.java:401) 
at org.jivesoftware.smackx.filetransfer.OutgoingFileTransfer.negotiateStream(OutgoingFileTransfer.java:359) 
at org.jivesoftware.smackx.filetransfer.OutgoingFileTransfer.access$100(OutgoingFileTransfer.java:35) 
at org.jivesoftware.smackx.filetransfer.OutgoingFileTransfer$2.run(OutgoingFileTransfer.java:214) 
at java.lang.Thread.run(Thread.java:1019) 
+0

請檢查此鏈接:http://stackoverflow.com/questions/13119371/用-asmack-or-any-alternatives發送文件,並下載這個文件asmack-jse-buddycloud-2010.12.11.jar並以這種方式實施。 –

回答

4

我不得不使用用於發送圖像到另一個用戶:

在按鈕點擊:

btn_transfer.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Intent takePic = new Intent(Intent.ACTION_PICK, 
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
      startActivityForResult(takePic, LOAD_IMAGE_GALLERY); 
     } 
    }); 

ActivityForResult:

 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    if (requestCode == LOAD_IMAGE_GALLERY && resultCode == RESULT_OK 
      && null != data) { 
     Uri selectedImage = data.getData(); 
     String[] filePathColumn = { MediaStore.Images.Media.DATA }; 

     Cursor cursor = getContentResolver().query(selectedImage, 
       filePathColumn, null, null, null); 
     cursor.moveToFirst(); 


     int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
     picturePath = cursor.getString(columnIndex); 
     cursor.close(); 

     try { 
      exifMedia = new ExifInterface(picturePath); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     String exifOrint = exifMedia.getAttribute(ExifInterface.TAG_ORIENTATION); 

     int exifOrientation = Integer.parseInt(exifOrint); 

     System.out.println("Orientation Tag is:"+exifOrientation); 

     System.out.println("Path Of the Image " + picturePath); 

     BitmapFactory.Options mOptions=new BitmapFactory.Options(); 
     mOptions.inSampleSize=2; 
     Bitmap imgBitmap = BitmapFactory.decodeFile(picturePath,mOptions); 
     Runtime.getRuntime().gc(); 

     imgBitmap = MyProfileActivity.getResizedBitmapImage(imgBitmap, 60, 60, exifOrientation); 
     MessageImageBitmap msgImgBitmap = new MessageImageBitmap(); 
     msgImgBitmap.setImgBitmap(imgBitmap); 
     msgImgBitmap.setImgPath(picturePath); 

     MessagePacket packetMsg = new MessagePacket(); 
     packetMsg.setMsgBitmap(msgImgBitmap); 
     packetMsg.setIsSendPacket(true); 
     packetMsg.setMessageType(MessagePacket.MESSAGE_TYPE_IMAGE); 
     packetMsg.setIMAGE_PATH(picturePath); 

     messageWithImage.add(packetMsg); 

     customAdapter1.notifyDataSetChanged(); 
     mList.setSelection(mList.getCount()); 

     String to = frienduserID; 
     dbhHelper.insertMessage(CCMStaticVariable.loginUserId 
       + "@ec.abc.com", refineFromjId(to), "", 
       "true", picturePath, 1); 

     sendFile(picturePath, frienduserID); 

    } 
} 

這爲我工作。我給你我的原始代碼,但你可以相應地改變它。讓我知道這是否有助於你。

+2

這是在你身邊工作嗎?我想知道爲什麼人們在沒有提供任何信息的情況下拒絕投票,爲什麼他們這樣做。我認爲當他們不知道答案時,它會激怒他們,他們會拒絕投票......哈哈:-D –

+0

請不要在您的答案中寫入不必要的代碼。如果提及任何其他類或方法,請提供代碼該類或方法也是如此。 – Dhananjay

+0

@非常感謝您的建議! –

2

此代碼傳輸文件..

FileTransferManager manager = new FileTransferManager(connection); 
OutgoingFileTransfer transfer = manager.createOutgoingFileTransfer("[email protected]/Smack"); 
File file = new File(filenameWithPath); 
try { 
    transfer.sendFile(file, "test_file"); 
} catch (XMPPException e) { 
    e.printStackTrace(); 
} 
while(!transfer.isDone()) { 
    if(transfer.getStatus().equals(Status.error)) { 
     System.out.println("ERROR!!! " + transfer.getError()); 
    } else if (transfer.getStatus().equals(Status.cancelled) 
        || transfer.getStatus().equals(Status.refused)) { 
     System.out.println("Cancelled!!! " + transfer.getError()); 
    } 
    try { 
     Thread.sleep(1000L); 
    } catch (InterruptedException e) { 
     e.printStackTrace(); 
    } 
} 
if(transfer.getStatus().equals(Status.refused) || transfer.getStatus().equals(Status.error) 
|| transfer.getStatus().equals(Status.cancelled)){ 
    System.out.println("refused cancelled error " + transfer.getError()); 
} else { 
    System.out.println("Success"); 
} 

另見..
http://harryjoy.com/2012/08/18/file-transfer-in-android-with-asmack-and-openfire/
filetransfer in android through xmpp?
Android File Transfer not working via XMPP and OpenFire

+0

是的,我已經做到了。並查看您提供的所有鏈接。 –