我正在为它创建一个带有 Android 客户端的桌面应用程序。目的是将图像从 Android 设备发送到桌面,然后,用户在桌面上执行轻拂手势后,将相同的图像发送回手机。
从 android 发送到桌面效果很好,但是当方法循环侦听端口(尝试接收图片)时,我在 LogCat 中收到很多“java.net.BindException:地址已在使用中”。这个异常一直在循环,但应用程序并没有崩溃。
这是负责从桌面发送数据的代码段:
public void sendImage(byte[] buffer)
{
try {
Socket s = new Socket("192.168.1.34",8080);
DataOutputStream dataOutputStream = null;
DataInputStream dataInputStream = null;
dataOutputStream = new DataOutputStream(s.getOutputStream());
dataInputStream = new DataInputStream(s.getInputStream());
dataOutputStream.writeInt(buffer.length);
dataOutputStream.write(buffer,0, buffer.length);
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
下面是我的 Android 应用程序的代码。当应用程序启动时,我在单独的线程上运行“ImageReceiver.class”。“ImageReceiver”是 Observable 对象,因此当它改变状态时,我可以启动新活动并显示图像。
public class FluidPhotoClientActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery gallery = (Gallery) findViewById(R.id.gallery);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
Toast.makeText(FluidPhotoClientActivity.this, "Sending photo nr: " + position, Toast.LENGTH_SHORT).show();
try {
Socket s = new Socket("192.168.1.35",8888);
DataOutputStream dataOutputStream = null;
DataInputStream dataInputStream = null;
dataOutputStream = new DataOutputStream(s.getOutputStream());
dataInputStream = new DataInputStream(s.getInputStream());
Resources res = getResources();
Drawable draw = null;
switch(position) // HARDCODED IMAGE POSITION
{
case 0:
draw = res.getDrawable(R.drawable.testimage);
break;
case 1:
draw = res.getDrawable(R.drawable.testimage2);
break;
}
if(draw!=null)
{
Bitmap bitmap = (Bitmap)((BitmapDrawable) draw).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] buffer = stream.toByteArray();
dataOutputStream.writeInt(buffer.length);
dataOutputStream.write(buffer,0, buffer.length);
s.close();
dataInputStream.close();
dataOutputStream.close();
} else {
Toast.makeText(FluidPhotoClientActivity.this, "Error", Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
Toast.makeText(FluidPhotoClientActivity.this, "IOException", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
});
ImageReceiver ir = new ImageReceiver();
Thread imageListener = new Thread(ir);
imageListener.start();
class ImageObserver implements Observer {
@Override
public void update(Observable arg0, Object arg1) {
Log.d("MESSAGE:" , "hello");
showIncomingImage();
}
}
ImageObserver io = new ImageObserver();
ir.addObserver(io);
}
public void showIncomingImage() {
startActivity(new Intent(this, SingleViewActivity.class));
}
}
ImageReceiver.class - 我想这是错误的来源。
public class ImageReceiver extends Observable implements Runnable {
private String responseImage;
public String getResponseImage() {
return responseImage;
}
@Override
public void run() {
while(1==1)
{
try {
ServerSocket ss = new ServerSocket(8080);
Socket s = ss.accept();
DataInputStream dataInputStream = new DataInputStream(s.getInputStream());
DataOutputStream dataOutputStream = new DataOutputStream(s.getOutputStream());
int imageSize = dataInputStream.readInt();
byte[] buffer = new byte[imageSize];
dataInputStream.readFully(buffer, 0, imageSize);
Log.d("MESSAGE:", String.valueOf(buffer.length));
ss.close();
s.close();
if(buffer.length > 0)
{
this.responseImage = String.valueOf(buffer.length);
setChanged();
notifyObservers();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
正如您在上面看到的,我正在使用不同的端口发送和接收数据,所以根据我的逻辑“地址不应该已经在使用中”——如果我理解正确的话。我正在尝试不同的端口,但这没有帮助。我将非常感谢所有提示。