Android ImageView未显示来自套接字连接的已接收图像

时间:2012-12-01 04:18:19

标签: android sockets imageview

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

1 个答案:

答案 0 :(得分:0)

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

我可能建议不要使用Drawable.createFromStream(),而是使用BitmapFactory.decodeStream()或先将数据手动下载到byte[]并使用BitmapFactory.decodeByteArray()。如果前者在收到数据时无法充分跟上解码器,那么后者有时很有用。

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