title: Render ByteBuffer into SurfaceView in Android
body: <p>I have an Android USB Camera application which will pump Camera frames to FrameAvaliable() method. The data retrieved from this method is a valid one (Confirmed by saving into raw file as image). I'm trying to render the frames from FrameAvaliable on SurfaceView created.</p>
  
  <pre><code>public class MainActivity extends Activity implements USBCameraListener{
  
  SurfaceView surfaceView;
  SurfaceHolder surfaceHolder;
  private Thread rendererThread;
  private boolean isStarted;
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView);
  
      surfaceHolder = surfaceView.getHolder();
      isStarted = false;
  
      surfaceHolder.addCallback(new SurfaceHolder.Callback() {
          @Override
          public void surfaceCreated(final SurfaceHolder holder) {
  
              surfaceHolderORG   =   holder;
  
              Log.d(TAG, "Surface Created");
  
              rendererThread =   new Thread(){
  
                  @Override
                  public void run() {
  
  
                      while(true) {
  
                          if (isStarted== true) {
                              //Added code
                              byte[] bytes = new byte[frameBuffer.remaining()];
                              int offset = 0, n = 0;
  
                              InputStream in = new ByteArrayInputStream(bytes);
  
                              try {
  
                                  while (offset &lt; bytes.length
                                          &amp;&amp; (n = in.read(bytes, offset, bytes.length - offset)) &gt;= 0) {
                                      offset += n;
  
                                  }
                                  in.close();
                              } catch (IOException e) {
                                  // TODO Auto-generated catch block
                                  e.printStackTrace();
                              }
  
  
                              // getting byte array
                              Bitmap bmp;
                              BitmapFactory.Options options = new BitmapFactory.Options();
                              options.inMutable = true;
                              bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
                              Canvas canvas = surfaceHolderORG.lockCanvas();
                              if (canvas != null) {
  
                                  canvas.drawBitmap(bmp, 0, 0, null);
                                  surfaceHolderORG.unlockCanvasAndPost(canvas); 
  
                              }
  
  
                          }
  
                      }
  
                  }
  
              };
  
  
  
              rendererThread.start();
          }
  
          @Override
          public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
  
          }
  
          @Override
          public void surfaceDestroyed(SurfaceHolder holder) {
  
          }
      });
  
  
      // Frame Listener
      @Override
      public void FrameAvailable(ByteBuffer byteBuffer) {
              frameBuffer = byteBuffer;
  
              if(isStarted == false){
                 startedProducer =   true;
              }
      }
  }
  
  }
  </code></pre>
  
  <p>When frames start to produce from FrameAvailable() , the condition check enters renderer code in rendererThread and the application throws the following exception and nothing is preview in SurfaceView.</p>
  
  <pre><code>12-26 03:05:20.590 12023-12104/com.sample.USBAPP D/skia: --- SkImageDecoder::Factory returned null
  12-26 03:05:20.590 3613-5139/? E/BufferQueueProducer: [SurfaceView] connect(P): already connected (cur=1 req=2)
  12-26 03:05:20.590 12023-12104/com.sample.USBAPP E/SurfaceHolder: Exception locking surface
  12-26 03:05:20.590 12023-12104/com.sample.USBAPP E/SurfaceHolder: java.lang.IllegalArgumentException
  12-26 03:05:20.590 12023-12104/com.sample.USBAPP E/SurfaceHolder:     at android.view.Surface.nativeLockCanvas(Native Method)
  </code></pre>
  
  <p>I don't know where i was going wrong.</p>