0
데스크톱에서 Android로 스크린 샷을 보내고 Android에서 표시하려고합니다. 내가 반환 null을 알면 InputStream.read()는 inputstream을 모두 읽지 않았으므로, 모든 것을 읽도록 while 루프를 만들었습니다. 데스크탑은 또한 안드로이드 세트 바이트 배열의 크기를 수 있도록하기 위해, 바이트 배열의 크기를 보내, 여기에 코드입니다 :아직 SkImageDecoder :: null가 입력 스트림을 모두 읽은 후 null을 반환했습니다.
public class connection extends AsyncTask {
private byte[] data;
public void setByteSize(int size) {
data = new byte[size];
}
public byte[] getByteSize() {
return data;
}
@Override
protected Object doInBackground(Object... arg0) {
try {
clientSocket = new Socket("134.129.125.126", 8080);
System.out.println("client connect to server");
input = clientSocket.getInputStream();
System.out.println("getinputstream");
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while (true) {
int totalBytesRead = 0;
int chunkSize = 0;
int tempRead = 0;
String msg = null;
// byte[] data = null;
byte[] tempByte = new byte[1024 * 1024 * 4];
try {
// read from the inputstream
tempRead = input.read(tempByte);
// tempbyte has x+4 byte
System.out.println("Fist time read:" + tempRead);
// convert x+4 byte into String
String message = new String(tempByte, 0, tempRead);
msg = message.substring(0, 4);
// cut the header into imageMsg
String imageMsg = new String(tempByte, 4, tempRead - 4);
// convert string into byte
System.out.println("message head:" + msg);
byteSize = Integer.parseInt(msg);
System.out.println("ByteSize:" + byteSize);
data = imageMsg.getBytes();
setByteSize(byteSize);
totalBytesRead = tempRead - 4;
System.out.println("total Byte Read=" + totalBytesRead);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// get string for the size of image
try {
int tmp = 0;
while (chunkSize > -1) {
System.out.println("data length:"
+ getByteSize().length);
chunkSize = input.read(getByteSize(), totalBytesRead,
getByteSize().length - totalBytesRead);
System.out.println("chunkSize is " + chunkSize);
System.out.println("iteration id = " + tmp);
tmp++;
totalBytesRead += chunkSize;
// System.out.println("Total byte read "
// + totalBytesRead);
if (totalBytesRead == getByteSize().length) {
break;
}
}
System.out.println("Complete reading - total read = "
+ totalBytesRead);
} catch (Exception e) {
}
bitmap = BitmapFactory.decodeByteArray(getByteSize(), 0,
getByteSize().length);
System.out.println("deco");
runOnUiThread(new Runnable() {
public void run() {
image.setImageBitmap(bitmap);
System.out.println("setImage at less than 500");
}
});
}
}
}
이 내 디코드 결과, 난 이미 그들 모두를 읽을 생각되지만, 여전히 이미지를 반환 할 수 없습니다.
10-21 23:25:15.203: I/System.out(7222): Fist time read:1452
10-21 23:25:15.203: I/System.out(7222): message head:3359
10-21 23:25:15.203: I/System.out(7222): ByteSize:3359
10-21 23:25:15.203: I/System.out(7222): total Byte Read=1448
10-21 23:25:15.203: I/System.out(7222): data length:3359
10-21 23:25:15.203: I/System.out(7222): chunkSize is 1911
10-21 23:25:15.203: I/System.out(7222): iteration id = 0
10-21 23:25:15.213: I/System.out(7222): Complete reading - total read = 3359
10-21 23:25:15.213: D/skia(7222): --- SkImageDecoder::Factory returned null