2

I am working on file transfer using xmpp. This is my code of file transfer.

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

The problem is that It crash when other side when accept the file.

Log file:

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)
Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
kalpana c
  • 2,739
  • 3
  • 27
  • 47
  • Please check this link: http://stackoverflow.com/questions/13119371/sending-files-with-asmack-or-any-alternatives and also download this file asmack-jse-buddycloud-2010.12.11.jar and implement this way. – kalpana c Dec 29 '12 at 10:19

2 Answers2

4

I had use this for sending image to another user:

On button click:

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

    }
}

This works for me.I am giving you my raw code but you can change it accordingly. Let me know if this helps you.

Akhilesh Mani
  • 3,502
  • 5
  • 28
  • 59
  • 2
    Is this working on your side? I wants to know why people down vote posts without giving any message, why they are doing so. I think when they don't know the answers , its irritate them and they do down vote ...LOL :-D – Akhilesh Mani Dec 28 '12 at 05:46
  • please don't write unnecessary code inside your answer.And If mentioning any other class or method please have a code of that class or method also. – DjP May 01 '14 at 08:46
2

This code transfers file..

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

Also see..
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

Community
  • 1
  • 1
ridoy
  • 6,274
  • 2
  • 29
  • 60