我想每次从相机捕获图像时,都会自动从我的android手机向服务器(PC)发送多个图像。使用InputStream通过TCP套接字接收多个图像
问题是read()
函数只是第一次阻塞。所以,从技术上讲,只有一张图像被接收并完美显示。但此后is.read()
返回-1
,此功能不会阻止和多个图像无法接收。
代码简单的服务器
while (true) {
InputStream is = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
is = sock.getInputStream();
if (is != null)
System.out.println("is not null");
int bufferSize = sock.getReceiveBufferSize();
byte[] bytes = new byte[bufferSize];
while ((count = is.read(bytes)) > 0)
{
if (filewritecheck == true)
{
fos = new FileOutputStream("D:\\fypimages\\image" + imgNum + ".jpeg");
bos = new BufferedOutputStream(fos);
imgNum++;
filewritecheck = false;
}
bos.write(bytes, 0, count);
System.out.println("count: " + count);
}
if (count <= 0 && bos != null) {
filewritecheck = true;
bos.flush();
bos.close();
fos.close();
}
}
后的图像输出收到的
is not null
is not null
is not null
is not null
is not null
is not null
is not null
is not null
...
...
...
...
任何帮助将得到高度赞赏。
任何线索的人? – Saaram 2013-04-24 10:29:55