2

我正在使用 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)
4

2 回答 2

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);
        }
    });

活动结果:

     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);

    }
}

这对我有用。我给你我的原始代码,但你可以相应地改变它。让我知道这是否对您有帮助。

于 2012-12-27T09:14:29.267 回答
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/
通过 xmpp 在 android 中传输文件?
Android 文件传输无法通过 XMPP 和 OpenFire 工作

于 2012-12-27T09:19:46.463 回答