我的目的是通过蓝牙socket发送一张图片,我是把图片转成byte[]类型发送的。接收端单步执行就可以收完图片,直接运行就接收不完了,困惑啊!
发送端代码:
OutputStream mOutStream = msocket.getOutputStream();
// 向socket对象所获取的流中发送数据
try {
Resources res=getResources();
Bitmap bitmap=BitmapFactory.decodeResource(res, user.imageId);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG,100,bos);
byte[] photo = bos.toByteArray();
mOutStream.write(photo);
mOutStream.flush();
} catch (IOException e) {e.printStackTrace(); }
接收端代码: if (socket != null) {
//从socket流中读取数据
try {
mmInStream = socket.getInputStream();
byte[] buffer = new byte[4096];
int temp = 0;
String str1 = "icon.png";
OutputStream os=receivecard.this.openFileOutput(str1,MODE_WORLD_READABLE);
while( (temp = mmInStream.read(buffer)) != -1) {
os.write(buffer, 0, temp);
}
System.out.println("传输结束"); //接收完后跳到名字叫ShowPicture的这个activity
Intent intent = new Intent(receivecard.this, ShowPicture.class);
startActivity(intent);
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
//此方法关闭socket
cancel();
}
} 现在的问题是,执行这一段代码时,不会自动结束。举个例子:假设我的图片有13523byte这么长,然后我每次接收4096byte,因为 13523 = 4096*3 + 1235 ,所以,在循环前三次时,temp的值均为4096,而在循环到第四次时,temp的值为1235,将这1235字节读完后,再去while中判断temp是否为-1.理论上说此时temp应该变为-1然后循环退出,但实际上此时temp一直是1235,循环退不出去,程序就死到这里了。不知道为啥??while( (temp = mmInStream.read(buffer)) != -1) {
os.write(buffer, 0, temp);
}以上是第一个问题。为了解决这个问题,我把代码改成了这样:
while( (temp = mmInStream.read(buffer)) != -1) {
os.write(buffer, 0, temp);
if(temp != 4096) break;
}
System.out.println("传输结束"); //接收完后跳到名字叫ShowPicture的这个activity
Intent intent = new Intent(receivecard.this, ShowPicture.class);
startActivity(intent);
加了一个if语句判断,这样最后一次即使temp没有变为-1,它也能跳出来,嘿嘿但是新问题又来了!!!!!经实际测试发现,这段代码,根本不能接收完全我的图片,可以说,大概只接受了图片的一个角,就打印出了传输结束,并开始执行下面的intent代码,跳转到下一个activity了!!于是我很困惑地单步执行,居然成功了!只有单步执行的时候才能接收完数据,直接全部执行的时候,只能收到图片的一个角,就跳到下面的代码了!这可怎么办??求高人指点迷津!小生感激不尽啊!~
发送端代码:
OutputStream mOutStream = msocket.getOutputStream();
// 向socket对象所获取的流中发送数据
try {
Resources res=getResources();
Bitmap bitmap=BitmapFactory.decodeResource(res, user.imageId);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG,100,bos);
byte[] photo = bos.toByteArray();
mOutStream.write(photo);
mOutStream.flush();
} catch (IOException e) {e.printStackTrace(); }
接收端代码: if (socket != null) {
//从socket流中读取数据
try {
mmInStream = socket.getInputStream();
byte[] buffer = new byte[4096];
int temp = 0;
String str1 = "icon.png";
OutputStream os=receivecard.this.openFileOutput(str1,MODE_WORLD_READABLE);
while( (temp = mmInStream.read(buffer)) != -1) {
os.write(buffer, 0, temp);
}
System.out.println("传输结束"); //接收完后跳到名字叫ShowPicture的这个activity
Intent intent = new Intent(receivecard.this, ShowPicture.class);
startActivity(intent);
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
//此方法关闭socket
cancel();
}
} 现在的问题是,执行这一段代码时,不会自动结束。举个例子:假设我的图片有13523byte这么长,然后我每次接收4096byte,因为 13523 = 4096*3 + 1235 ,所以,在循环前三次时,temp的值均为4096,而在循环到第四次时,temp的值为1235,将这1235字节读完后,再去while中判断temp是否为-1.理论上说此时temp应该变为-1然后循环退出,但实际上此时temp一直是1235,循环退不出去,程序就死到这里了。不知道为啥??while( (temp = mmInStream.read(buffer)) != -1) {
os.write(buffer, 0, temp);
}以上是第一个问题。为了解决这个问题,我把代码改成了这样:
while( (temp = mmInStream.read(buffer)) != -1) {
os.write(buffer, 0, temp);
if(temp != 4096) break;
}
System.out.println("传输结束"); //接收完后跳到名字叫ShowPicture的这个activity
Intent intent = new Intent(receivecard.this, ShowPicture.class);
startActivity(intent);
加了一个if语句判断,这样最后一次即使temp没有变为-1,它也能跳出来,嘿嘿但是新问题又来了!!!!!经实际测试发现,这段代码,根本不能接收完全我的图片,可以说,大概只接受了图片的一个角,就打印出了传输结束,并开始执行下面的intent代码,跳转到下一个activity了!!于是我很困惑地单步执行,居然成功了!只有单步执行的时候才能接收完数据,直接全部执行的时候,只能收到图片的一个角,就跳到下面的代码了!这可怎么办??求高人指点迷津!小生感激不尽啊!~
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货