gpt4 book ai didi

android - 使用 xmpp 在 Android 中传输文件

转载 作者:太空狗 更新时间:2023-10-29 16:21:17 25 4
gpt4 key购买 nike

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

最佳答案

我曾用它来将图像发送给另一个用户:

点击按钮:

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

Activity 结果:

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

}
}

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

关于android - 使用 xmpp 在 Android 中传输文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14051733/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com