我试图通过python服务器的套接字向android客户端发送一个文件(png特定)。我知道我的python服务器正在发送数据,我只是无法弄清楚如何在android端接收数据。以下是接收文件的代码。
String path = Environment.getExternalStorageDirectory().toString() +"/tmp/test.png";
try {
socket = new Socket("192.168.1.129", 29877);
is = socket.getInputStream();
out = new FileOutputStream(path);
byte[] temp = new byte[1024];
for(int c = is.read(temp,0,1024); c > 0; c = is.read(temp,0,1024)){
out.write(temp,0,c);
Log.d("debug tag", out.toString());
}
Log.d("debug tag", temp.toString());
Bitmap myBitmap = BitmapFactory.decodeByteArray(temp, 0, temp.length);
imageView.setImageBitmap(myBitmap);
感谢您的任何建议。
答案 0 :(得分:1)
您正在以1K块的形式读取套接字并将其保存到文件中。然后,您尝试将最后一个块解释为位图。这不起作用。
保存后从文件中读取图像,或将其全部缓存在内存中。