发送图片
图片放在asset目录下
public void sendImage(View view)
{
try {
//读出图
AssetManager manager=getAssets();
InputStream is=
manager.open( "first_logo.png");
//把图转成文本
int size=is.available();
byte[] data=new byte[size];
is.read(data);
//字符编码 0001 a 0008 a
//String str=new String(data);
//好友iphone 6 plus 收到后
//str.getBytes();
String body=Base64.encodeToString(data,
Base64.DEFAULT);
//加上信息表示发的是图
body="image"+body;
//发送
TApplication.multiuserchat
.sendMessage(body);
} catch (Exception e) {
}
}
发送文字
sendbtn = (Button) findViewById(R.id.send);
bodyet = (EditText) findViewById(R.id.body);
myreceive = new Myreceive();
this.registerReceiver(myreceive, new IntentFilter("show"));
linearlayout = (LinearLayout) findViewById(R.id.ll_chatContent);
sendbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
String body = bodyet.getText().toString();
TApplication.multiuserchat.sendMessage("android" + body);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
TApplication中的程序
new Thread(){
public void run() {
try {
ConnectionConfiguration config=new ConnectionConfiguration(
"172.60.6.73",5222,"tarena.com");
xmpp=new XMPPConnection(config);
MyPacklistener packetListener=new MyPacklistener();
xmpp.addPacketListener(packetListener, null);
xmpp.connect();
xmpp.login("juexinbugai", "lizhenjing");
//加入群聊
multiuserchat=new MultiUserChat(xmpp, "gsd1602@conference.tarena.com");
multiuserchat.join("1213142");
//在android加入成功,电脑的spark上能看到
} catch (Exception e) {
e.printStackTrace();
}
};
}.start();
class MyPacklistener implements PacketListener{
@Override
public void processPacket(Packet packet) {
if (packet instanceof Message) {
Message msg=(Message) packet;
String who=msg.getFrom();
String body=msg.getBody();
//发广播
Intent intent=new Intent("show");
intent.putExtra("who", who);
intent.putExtra("body", body);
sendBroadcast(intent);
}
}
}