有人知道为什么该方法不返回位图?而不是位图,从异常返回null。此方法在另一个类中调用(在新线程中)。
方法代码:
class ClientIn {
Socket socket;
byte[] lenBuffer;
ReceiveBuffer buffer;
public ClientIn(Socket s) {
socket = s;
lenBuffer = new byte[4];
}
public Bitmap Receive() {
try {
InputStream stream = socket.getInputStream();
stream.read(lenBuffer,0,lenBuffer.length);
buffer = new ReceiveBuffer(BitConverter.ToInt32(lenBuffer,0)) ;
while(buffer.toReceive > 0) {
stream.read(buffer.Buffer, 0, buffer.Buffer.length);
if (buffer.Buffer.length <= 0) {
continue;
}
else {
if (buffer.toReceive < 1024) {
buffer.bufStream.write(buffer.Buffer, 0, buffer.toReceive);
buffer.toReceive -= buffer.toReceive;
Arrays.fill(buffer.Buffer, 0, buffer.Buffer.length, (byte) 0);
}
else {
buffer.bufStream.write(buffer.Buffer, 0, buffer.Buffer.length);
buffer.toReceive -= buffer.Buffer.length;
if (buffer.toReceive > 0) {
Arrays.fill(buffer.Buffer, 0, buffer.Buffer.length, (byte) 0);
}
}
}
}
byte[] test123 = buffer.bufStream.toByteArray();
Bitmap thtImg2 = BitmapFactory.decodeByteArray(test123, 0, test123.length);
return thtImg2; // <--- INSTEAD OF BITMAP, METHOD RETURN NULL
}
catch (IOException e) {
e.printStackTrace();
return null;
}
} }
通过套接字接收的图像数据作为字节数组。然后按BitmapFactory.decodeByteArray (test123, 0, test123.length);
或位图接收错误数据或已解码错误。结果如下: