我想每次從相機捕獲圖像時,都會自動從我的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