发送的android BMP使用套接字 [英] android sending bmp using sockets

查看:106
本文介绍了发送的android BMP使用套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用套接字来发送bmp图像。我有这样code在Android:

I'm trying to send bmp image using socket. I have such code on android:

ByteArrayOutputStream stream = new ByteArrayOutputStream();
MainActivity.bmp.compress(Bitmap.CompressFormat.JPEG, 20,
                stream);
byte[] byteArray = stream.toByteArray();
OutputStream os = echoSocket.getOutputStream();
os.write(byteArray,0,byteArray.length);
os.flush();

在PC上:

InputStream in_ = clientSocket.getInputStream();
OutputStream out_ = new FileOutputStream("filename.bmp");
final byte[] buffer = new byte[1024];
int read = -1;
int i = 0;
while ((read = in_.read(buffer)) != -1) {
out_.write(buffer, 0, read); 
System.out.println(i);
i++;

}

in_.close();
out_.close();


System.out.println("Done");

它永远不会获取最后一行(的println(完成))。它只做当我关闭Android程序,它获取到最后一行和BMP成功地打开。问题是in_.read等待Android已经发送完毕后,我不能使它工作。

It never gets to last line( println("Done") ). It only does when I close android program, it gets to last line and bmp opens succesfully. problem is in_.read waits after android has finished transmitting, and i can't make it work.

推荐答案

您永远关闭在设备端的插座/的OutputStream所以PC端不知道有没有更多的数据,所以它只是旋转的同时,循环读取0字节一次。

You never close the socket/OutputStream on the device side so the PC side doesn't know that there is no more data and so it just spins in the while loop reading 0 bytes at a time.

另外,如果你打算使用我的解决方案,请注明我,在你的<一个公认的答案href=\"http://stackoverflow.com/questions/16884396/sending-bmp-usng-sockets/16884678#16884678\">$p$pvious线程。

Also, if you're going to use my solution, please mark me as the accepted answer in your previous thread.

这篇关于发送的android BMP使用套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆