title: Android Camera Image Capture without button
body: <p>Ok I have been having trouble implementing a camera application that is able to capture an image without the press of a button. Currently a working camera application but when the below code is commented out:</p>
  
  <pre><code>buttonClick = (Button) findViewById(R.id.buttonClick);
  
  buttonClick.setOnClickListener(new OnClickListener() {
    public void onClick(View v) { // &lt;5&gt;
      (1)    preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);
    }
  });*/
  </code></pre>
  
  <p>and replaced with a loop that just calls (1), the preview is never visible and causes the application to shutdown. Preview class is below:</p>
  
  <pre><code>import java.io.IOException;
  import android.content.Context;
  import android.hardware.Camera;
  import android.hardware.Camera.PreviewCallback;
  
  import android.util.Log;
  
  import android.view.SurfaceHolder;
  
  import android.view.SurfaceView;
  
  class Preview extends SurfaceView implements SurfaceHolder.Callback { // 
  
    &lt;1&gt;
      private static final String TAG = "Preview";
  
    SurfaceHolder mHolder;  // &lt;2&gt;
    public Camera camera; // &lt;3&gt;
  
    Preview(Context context) {
      super(context);
  
      // Install a SurfaceHolder.Callback so we get notified when the
      // underlying surface is created and destroyed.
      mHolder = getHolder();  // &lt;4&gt;
      mHolder.addCallback(this);  // &lt;5&gt;
      mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); // &lt;6&gt;
    }
  
    // Called once the holder is ready
  
    public void surfaceCreated(SurfaceHolder holder) {  // &lt;7&gt;
      // The Surface has been created, acquire the camera and tell it where
      // to draw.
      camera = Camera.open(); // &lt;8&gt;
      try {
        camera.setPreviewDisplay(holder);  // &lt;9&gt;
  
        camera.setPreviewCallback(new PreviewCallback() { // &lt;10&gt;
          // Called for each frame previewed
          public void onPreviewFrame(byte[] data, Camera camera) {  // &lt;11&gt;
            Log.d(TAG, "onPreviewFrame called at: " + System.currentTimeMillis());
            Preview.this.invalidate();  // &lt;12&gt;
          }
        });
      } catch (IOException e) { // &lt;13&gt;
        e.printStackTrace();
      }
    }
  
    // Called when the holder is destroyed
    public void surfaceDestroyed(SurfaceHolder holder) {  // &lt;14&gt;
      camera.stopPreview();
      camera = null;
    }
  
    // Called when holder has changed
    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { // &lt;15&gt;
      camera.startPreview();
    } 
  
  }
  </code></pre>
  
  <p>Any ideas as to where to go from here and why this is occuring?</p>