使用xmpp在Android中进行文件传输

时间:2012-12-27 08:39:29

标签: android xmpp

我正在使用xmpp进行文件传输。这是我的文件传输代码。

 ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
if (sdm == null)
    sdm = new ServiceDiscoveryManager(connection);
FileTransferManager manager = new FileTransferManager(connection);
OutgoingFileTransfer transfer = manager.createOutgoingFileTransfer("user@myHost/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)

2 个答案:

答案 0 :(得分: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);

    }
}

这对我有用。我给你的原始代码,但你可以相应地改变它。如果这有助于您,请告诉我。

答案 1 :(得分:2)

此代码传输文件..

FileTransferManager manager = new FileTransferManager(connection);
OutgoingFileTransfer transfer = manager.createOutgoingFileTransfer("usre2@myHost/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