如何在android中发送audio files
像whatsapp
这样的最佳方法是什么?
还如何发送视频和照片? 正在遵循最好的方式
在android asmack中发送文件的最佳方式是什么
public void send()
{
configureProviderManager(connection);
FileTransferNegotiator.setServiceEnabled(connection, true);
FileTransferManager manager = new FileTransferManager(connection);
OutgoingFileTransfer transfer = manager.createOutgoingFileTransfer("xxxxx@jabber.ccc.de/Smack");
File file = new File("/sdcard/DCIM/Camera/1385869353956.jpg");
try {
Log.d("file sending",file.getAbsolutePath()+" "+file.getName());
configureProviderManager(connection);
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))
System.out.println("refused " + transfer.getError());
else if( transfer.getStatus().equals(Status.error))
System.out.println(" error " + transfer.getError());
else if(transfer.getStatus().equals(Status.cancelled))
System.out.println(" cancelled " + transfer.getError());
else
System.out.println("Success");
}
答案 0 :(得分:1)
您可以根据自己的要求进行更改。
public class MyFileTransfer extends SmackTest<XMPPTCPConnection> {
public static void main(String args[]) throws Exception {
SmackTest<XMPPTCPConnection> test = new MyFileTransfer();
test.runTest();
}
private static final byte[] dataToSend = StringUtils.randomString(1024 * 4 * 3).getBytes();
private static byte[] dataReceived;
@Override
protected void runTestSubclass() throws SmackException, IOException,
XMPPException, InterruptedException, KeyManagementException, NoSuchAlgorithmException {
FileTransferNegotiator.IBB_ONLY = true;
XMPPTCPConnectionConfiguration.Builder conf = XMPPTCPConnectionConfiguration.builder();
conf.setServiceName(SERV);
conf.setUsernameAndPassword(USER, PASS);
conf.setSecurityMode(SecurityMode.disabled);
conf.setCompressionEnabled(true);
TLSUtils.acceptAllCertificates(conf);
conf.setResource("sender");
connection = new XMPPTCPConnection(conf.build());
connection.connect();
connection.login();
conf.setResource("receiver");
XMPPTCPConnection connection2 = new XMPPTCPConnection(conf.build());
connection2.connect();
connection2.login();
FileTransferManager ftm1 = FileTransferManager.getInstanceFor(connection);
FileTransferManager ftm2 = FileTransferManager.getInstanceFor(connection2);
ftm2.addFileTransferListener(new FileTransferListener() {
@Override
public void fileTransferRequest(FileTransferRequest request) {
IncomingFileTransfer ift = request.accept();
try {
InputStream is = ift.recieveFile();
ByteArrayOutputStream os = new ByteArrayOutputStream();
int nRead;
byte[] buf = new byte[1024];
while ((nRead = is.read(buf, 0, buf.length)) != -1) {
os.write(buf, 0, nRead);
}
os.flush();
dataReceived = os.toByteArray();
} catch (SmackException | IOException | XMPPErrorException e) {
e.printStackTrace();
}
if (Arrays.equals(dataToSend, dataReceived)) {
System.out.println("Received data matches send data. \\o/");
} else {
System.err.println("Recieved data DOES NOT match send data. :(");
}
}
});
OutgoingFileTransfer oft = ftm1.createOutgoingFileTransfer(XmppStringUtils.completeJidFrom(USER, SERV, "receiver"));
oft.sendStream(new ByteArrayInputStream(dataToSend), "hello.txt", dataToSend.length, "A greeting");
outerloop: while (!oft.isDone()) {
switch (oft.getStatus()) {
case error:
System.out.println("Filetransfer error: " + oft.getError());
break outerloop;
default:
System.out.println("Filetransfer status: " + oft.getStatus() + ". Progress: " + oft.getProgress());
break;
}
Thread.sleep(1000);
}
connection.disconnect();
connection2.disconnect();
Thread.sleep(1000);
}
}