0

我试图在 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);
               }
            }
         }
    }
 }
4

1 回答 1

0

听起来您遇到的问题是在接收数据后将数据解码为正确的图像。

我可能建议不要使用Drawable.createFromStream(),而是使用BitmapFactory.decodeStream()或手动将数据下载到byte[]第一个并使用BitmapFactory.decodeByteArray(). 有时,如果前者在接收数据时不能充分跟上解码器的速度,则后者很有用。

您当前使用的方法实际上最终调用了BitmapFactory.descodeResourceStream(),它用于从本地 res/ 包中读取图像数据,而不是从远程套接字中读取图像数据。

于 2012-12-01T04:33:28.513 回答