我试图在 Android 应用程序中显示两个连续的图像,这些图像是通过套接字接收的(从使用 sendfile 的 C 程序)。发件人代码似乎工作正常,我在 Android 代码方面遇到了问题。
Android应用程序中的部分代码如下:
public class DisplayNewActivity extends Activity {
...
@Override
public void onCreate(Bundle savedInstanceState) {
....
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
Drawable d2;
d2 = (Drawable)msg.obj;
imageSock.setImageDrawable(d2); // to set the arrived image in the imageshow object
}
};
...
cThread = new Thread(new ClientThread());
rThread = new Thread(new RcvThread());
cThread.start();
}
public class ClientThread implements Runnable {
public void run() {
// thread used for socket connection.
...
rThread.start(); // once the connection has been established
...
}
}
@SuppressLint("HandlerLeak")
public class RcvThread implements Runnable {
public void run() {
while (connected) {
try {
InputStream inputStream = socket.getInputStream();
Drawable d = Drawable.createFromStream(inputStream, null);
Message msg = new Message();
msg.obj = d;
mHandler.sendMessage(msg);
} catch (Exception e) {
Log.e("SocketConnectionv02Activity", "C: ErrorRCVD", e);
}
}
}
}
}
我面临的问题是,在正确显示 Android 应用程序接收到的第一张图像后,不显示接收到的下一张(正确接收到的)图像并留下显示空白的 imageview 对象。
有什么建议/想法来解决这个问题吗?
提前感谢您提供的任何帮助。
编辑:
public class DisplayNewActivity extends Activity {
...
@Override
public void onCreate(Bundle savedInstanceState) {
....
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
Bitmap d2;
d2 = (Bitmap)msg.obj;
imageSock.setImageBitmap(d2); // to set the arrived image in the imageshow object
}
};
...
cThread = new Thread(new ClientThread());
rThread = new Thread(new RcvThread());
cThread.start();
}
public class ClientThread implements Runnable {
public void run() {
// thread used for socket connection.
...
rThread.start(); // once the connection has been established
...
}
}
@SuppressLint("HandlerLeak")
public class RcvThread implements Runnable {
public void run() {
while (connected) {
try {
DataInputStream in = new DataInputStream(socket.getInputStream());
Bitmap d = BitmapFactory.decodeStream(in);
Message msg = new Message();
msg.obj = d;
mHandler.sendMessage(msg);
} catch (Exception e) {
Log.e("SocketConnectionv02Activity", "C: ErrorRCVD", e);
}
}
}
}
}