该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
public class MainActivity extends Activity {
public ImageView image;
private Bitmap bitmap;
private static final int COMPLETED = 0;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == COMPLETED) {
image.setImageBitmap(bitmap);
super.handleMessage(msg);
}
}
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.send_image1);
Thread th = new MyThread();
th.start();
System.out.println("thread start!");
}
class MyThread extends Thread {
public Socket s;
public ServerSocket ss;
public void run() {
byte[] buffer = new byte[1024];
int len = 0;
Message msg = new Message();
try {
ss = new ServerSocket(8889);
System.out.println("ServerSocket is created!");
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
while (true) {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
InputStream ins = null;
try {
s = ss.accept();
ins = s.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
try {
while ((len = ins.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
} catch (IOException e1) {
e1.printStackTrace();
}
byte data[] = outStream.toByteArray();
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
msg.what = COMPLETED;
handler.sendMessage(msg);
try {
ins.close();
outStream.flush();
outStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}