1
我想从python服务器到android客户端的套接字发送一个文件(png是具体的)。我知道我的python服务器正在发送数据,我只是无法弄清楚如何在android端接收数据。这是代码看起来像接收文件。android tcp客户端文件接收
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);
感谢您的任何建议。
我如何将它全部缓冲在内存中?例? – user1661396
你可以使用'java.io.ByteArrayOutputStream'类似的东西,http://docs.oracle.com/javase/6/docs/api/java/io/ByteArrayOutputStream.html –
哇,老兄,谢谢,我不能相信我didn我没有意识到我只想读最后一个大块......愚蠢......非常感谢。 – user1661396