title: Correct way to send onPreviewFrame() byte[] with Sockets
body: <p>I'm trying to send the preview frame from a phone's camera to a tablet. In the server app I start a thread from <code>onPreviewFrame()</code> to establish a ServerSocket which in turn starts another thread that responds to button click, for verification purposes, from the client app. Like so...</p>
  
  <pre><code>    @Override
      public void onPreviewFrame(byte[] data, Camera camera) {
          Log.i("Server", "onPreviewFrame() called");
          mParent.new SocketServerThread(data).start();   
      } 
  </code></pre>
  
  <p>where SocketServerThread does...</p>
  
  <pre><code>     ...
       private mData = data;
       ...
       @Override
       public void run() {
           try {
               serverSocket = new ServerSocket(SocketServerPORT);
  
               while (true) {
                   Socket socket = serverSocket.accept();
                   SocketServerReplyThread socketServerReplyThread = 
                           new SocketServerReplyThread(socket, count, mData);
                   Log.i("Server", "data array sent to reply thread");
                   socketServerReplyThread.run();
               }
           } catch (IOException e) {
               e.printStackTrace();
           }
       }
   }
  </code></pre>
  
  <p>where I'm able to verify the connection via the block on <code>serverSocket.accept()</code>. The fun begins trying to deal with the data. One attempt is like so...</p>
  
  <pre><code>     @Override
       public void run() {
           try {
               outputStream = hostThreadSocket.getOutputStream();
               DataOutputStream dataStream = new DataOutputStream(outputStream);
               dataStream.writeInt(mBuffer.length);
               if (mBuffer.length &gt; 0)
                   outStream.write(mBuffer);
               outStream.close();
  
           } catch (IOException e) {
               e.printStackTrace();
           }
       }
   }
  </code></pre>
  
  <p>I've set breakpoint and everything seems to go correctly except for <code>outStream.close()</code>'s breakpoint never gets hit. Instead it returns to my breakpoint on <code>Socket socket = serverSocket.accept()</code>.</p>
  
  <p>On the client side I have an <code>AsyncTask</code> that handles the connection, invoked by the onClick, like so...</p>
  
  <pre><code> @Override
   protected Void doInBackground(Void... arg0) {
  
       Socket socket = null;
       int bytesRead;
  
       try {
           socket = new Socket(dstAddress, dstPort);
  
           InputStream inputStream = socket.getInputStream();
           DataInputStream dis = new DataInputStream(inputStream);                     
  
           bytesRead = dis.read(buffer);
           if (bytesRead != -1) {
               final Bitmap bitmap = BitmapFactory.decodeByteArray(buffer, 0, bytesRead);
               final DisplayMetrics metrics = new DisplayMetrics();
  
               AndroidClientActivity.this.runOnUiThread(new Runnable() {
  
                  @Override
                  public void run() {
                      getWindowManager().getDefaultDisplay().getMetrics(metrics);
                      //view is an ImageView
                      view.setMinimumHeight(metrics.heightPixels);
                      view.setMinimumWidth(metrics.widthPixels);
                      view.setImageBitmap(bitmap);
                      view.refreshDrawableState();
                  }
              });     
           }
  
       } catch (UnknownHostException e) {
           e.printStackTrace();
       } catch (IOException e) {
           e.printStackTrace();
       }finally{
           if(socket != null){
               try {
                   socket.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
       }
       return null;
   }
  </code></pre>
  
  <p>The problem is the <code>ImageView</code> doesn't show any frames. </p>
  
  <p>Question(s):</p>
  
  <ul>
  <li>
  Is there something else that needs to be done to the byte[] prior to setting the ImageView with the byte[]?
  </li>
  <li>
  Should the processing of the byte[] be done in the server app instead; does it matter?
  </li>
  <li>
  Is there an overall better approach to this?
  </li>
  </ul>
  
  <p>I should mention that the previous code is a combo of some tutorials changed and meshed with my own work. I've tried achieving the same with alternate streams such as <code>ByteArray</code> and raw streams with no luck. I've found I get undependable feedback when debugging both apps at the same time and when running one while debugging the other only gives me a partial view into the problems. When I debug both I get NPEs in random places that don't occur otherwise; possibly due to pausing by the breakpoints. Not sure though. </p>