就是在发送图片的时候会断开与服务器的链接,并且消息也会发送失败。09-22 10:20:24.071: I/System.out(10456): 10:20:24 上午 RCV (1079209600): <stream:error xmlns:stream="http://etherx.jabber.org/streams"><internal-server-error xmlns="urn:ietf:params:xml:ns:xmpp-streams"/></stream:error>我想知道,是因为服务器的原因呢,还是本身代码的问题呢,我是把图片转换成字节码,然后再把字节码转换成16进制,然后当做消息发送出去的。这样做有问题吗?
发送:
String msg=MyApplication.encode(MyApplication.Bitmap2Bytes(bmp));
Message mes= new Message();
mes.setBody(msg);
newchat.sendMessage(mes);
接收:Bitmap bm = MyApplication.Bytes2Bimap(MyApplication
.decode(body));
body="/mnt/sdcard/imags"+ TimeRender.getDatejpg() + ".jpg";
File f = new File(body);
BufferedOutputStream bos;
try {
bos = new BufferedOutputStream(
new FileOutputStream(f));
bm.compress(Bitmap.CompressFormat.JPEG, 100, bos);
bos.flush();
bos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}转换方法:
/*
* 将字符串编码成16进制数字,适用于所有字符(包括中文)
*/
public static String encode(byte[] bytes ) {
// 根据默认编码获取字节数组
// byte[] bytes = str.getBytes();
StringBuilder sb = new StringBuilder(bytes.length * 2);
// 将字节数组中每个字节拆解成2位16进制整数
for (int i = 0; i < bytes.length; i++) {
sb.append(hexString.charAt((bytes[i] & 0xf0) >> 4));
sb.append(hexString.charAt((bytes[i] & 0x0f) >> 0));
}
return sb.toString();
} /*
* 将16进制数字解码成字符串,适用于所有字符(包括中文)
*/
public static byte[] decode(String bytes) {
ByteArrayOutputStream baos = new ByteArrayOutputStream(
bytes.length() / 2);
// 将每2位16进制整数组装成一个字节
for (int i = 0; i < bytes.length(); i += 2)
baos.write((hexString.indexOf(bytes.charAt(i)) << 4 | hexString
.indexOf(bytes.charAt(i + 1))));
return baos.toByteArray();
}
//将图片转化成字节
public static byte[] Bitmap2Bytes(Bitmap bm){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
//将字节转化成图片
public static Bitmap Bytes2Bimap(byte[] b){
if(b.length!=0){
return BitmapFactory.decodeByteArray(b, 0, b.length);
}
else {
return null;
}
}
发送:
String msg=MyApplication.encode(MyApplication.Bitmap2Bytes(bmp));
Message mes= new Message();
mes.setBody(msg);
newchat.sendMessage(mes);
接收:Bitmap bm = MyApplication.Bytes2Bimap(MyApplication
.decode(body));
body="/mnt/sdcard/imags"+ TimeRender.getDatejpg() + ".jpg";
File f = new File(body);
BufferedOutputStream bos;
try {
bos = new BufferedOutputStream(
new FileOutputStream(f));
bm.compress(Bitmap.CompressFormat.JPEG, 100, bos);
bos.flush();
bos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}转换方法:
/*
* 将字符串编码成16进制数字,适用于所有字符(包括中文)
*/
public static String encode(byte[] bytes ) {
// 根据默认编码获取字节数组
// byte[] bytes = str.getBytes();
StringBuilder sb = new StringBuilder(bytes.length * 2);
// 将字节数组中每个字节拆解成2位16进制整数
for (int i = 0; i < bytes.length; i++) {
sb.append(hexString.charAt((bytes[i] & 0xf0) >> 4));
sb.append(hexString.charAt((bytes[i] & 0x0f) >> 0));
}
return sb.toString();
} /*
* 将16进制数字解码成字符串,适用于所有字符(包括中文)
*/
public static byte[] decode(String bytes) {
ByteArrayOutputStream baos = new ByteArrayOutputStream(
bytes.length() / 2);
// 将每2位16进制整数组装成一个字节
for (int i = 0; i < bytes.length(); i += 2)
baos.write((hexString.indexOf(bytes.charAt(i)) << 4 | hexString
.indexOf(bytes.charAt(i + 1))));
return baos.toByteArray();
}
//将图片转化成字节
public static byte[] Bitmap2Bytes(Bitmap bm){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
//将字节转化成图片
public static Bitmap Bytes2Bimap(byte[] b){
if(b.length!=0){
return BitmapFactory.decodeByteArray(b, 0, b.length);
}
else {
return null;
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货