我试图发送两个android设备之间的图片,但有一个传输问题,我想不出。有人告诉我修改这个可疑的循环,但它仍然不起作用。 当我在设备上测试我的项目时,连接没有问题。但是,随着传输任务的开始,发送方客户端被停止,接收方显示“传输错误”消息。 有没有人知道我可以对我的程序做些什么?这里是我发送和接收的两个主要部分。如何正确接收插座
我会非常感谢任何帮助。谢谢。
发送部分:
s = new Socket("192.168.0.187", 1234);
Log.d("Tag====================","socket ip="+s);
File file = new File("/sdcard/DCIM/Pic/img1.jpg");
FileInputStream fis = new FileInputStream(file);
din = new DataInputStream(new BufferedInputStream(fis));
dout = new DataOutputStream(s.getOutputStream());
dout.writeUTF(String.valueOf(file.length()));
byte[] buffer = new byte[1024];
int len = 0;
while ((len = din.read(buffer)) != -1) {
dout.write(buffer, 0, len);
tw4.setText("8 in while dout.write(buffer, 0, len);");
}
dout.flush();
发送部分可以顺利工作,没有埃罗出现了while循环痊愈了
后接收部分:
try {
File file = new File("/sdcard/DCIM/img1.jpg");
DataInputStream din = new DataInputStream(new BufferedInputStream(client.getInputStream()));
bis = new BufferedInputStream(client.getInputStream());
Log.d("Tag====================","din="+s);
FileOutputStream fos = new FileOutputStream(file);
dout = new DataOutputStream(new BufferedOutputStream(fos));
byte[] buffer = new byte[1024];
int len = 0;
while ((len = bis.read(buffer)) != -1) {
dout.write(buffer, 0, len);
}
dout.flush();
dout.close();
} catch (Exception e) {
handler.post(new Runnable() {
public void run() {
tw1.setText("transmission error");
}});
容纳部分周围似乎连卡在“DataInputStream din = new DataInputStream(new BufferedInputStream(client.getInputStream()));”并抓住例外。
再次感谢。
请提供catched异常的堆栈跟踪。 – flx
08-26 22:49:25.950:D/OpenGLRenderer(18754):启用调试模式0 08-26 22:54:00.510:D/libEGL(19098):loaded /system/lib/egl/libEGL_tegra.so 08/26 22:54:00.530:D/libEGL(19098):loaded /system/lib/egl/libGLESv1_CM_tegra.so 08-26 22:54:00.540:D/libEGL(19098):loaded/system/lib/egl/libGLESv2_tegra.so 08-26 22:54:00.570:D/OpenGLRenderer(19098):启用调试模式0 这里是logcat,谢谢 –
嗯,你没有记录异常。所以它不是在logcat .. – flx