title: How to open default camera and take a photo automatically in android
body: <p>I am new to <code>cordova</code>. working on ble device project.</p>
  
  <p>My requirement is,</p>
  
  <ul>
  <li>First click on  ble device should  open the camera.</li>
  <li>next corresponding clicks to take photos .</li>
  </ul>
  
  <blockquote>
    <p>AutomaticPhotoActivity.java   </p>
  </blockquote>
  
  <pre><code>import java.io.File;
  import java.io.FileNotFoundException;
  import java.io.FileOutputStream;
  import java.io.IOException;
  
  import android.app.Activity;
  import android.content.Intent;
  import android.graphics.Bitmap;
  import android.graphics.BitmapFactory;
  import android.graphics.SurfaceTexture;
  import android.hardware.Camera;
  import android.os.Bundle;
  import android.os.CountDownTimer;
  import android.os.Environment;
  import android.util.Log;
  import android.view.SurfaceView;
  import android.view.View;
  import android.widget.TextView;
  
  public class AutomaticPhotoActivity extends Activity{
      private Camera camera; // camera object
      private TextView textTimeLeft; // time left field
  
  
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          textTimeLeft=(TextView)findViewById(R.id.textTimeLeft); // make time left object
  
        /*  Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
          startActivityForResult(intent, 0);*/
              camera = Camera.open(0);
          try {
              SurfaceView view = new SurfaceView(this);
  
  
              camera.setPreviewDisplay(view.getHolder()); // feed dummy surface to surface
              SurfaceTexture st = new SurfaceTexture(MODE_PRIVATE);
              camera.setPreviewTexture(st);
  
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  
      Camera.PictureCallback jpegCallBack=new Camera.PictureCallback() {
          public void onPictureTaken(byte[] data, Camera camera) {
              // set file destination and file name
              File destination=new File(Environment.getExternalStorageDirectory(),"myPicture.jpg");
              try {
                  Bitmap userImage = BitmapFactory.decodeByteArray(data, 0, data.length);
                  // set file out stream
                  FileOutputStream out = new FileOutputStream(destination);
                  // set compress format quality and stream
                  userImage.compress(Bitmap.CompressFormat.JPEG, 90, out);
  
              } catch (FileNotFoundException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
  
          }
      };
  //  onclick off button wait for 5seconds then take phot
      public void startTimer(View v){
  
          // 5000ms=5s at intervals of 1000ms=1s so that means it lasts 5 seconds
          new CountDownTimer(5000,1000){
  
              @Override
              public void onFinish() {
                  // count finished
                  textTimeLeft.setText("Picture Taken");
                  camera.takePicture(null, null, null, jpegCallBack);
              }
  
              @Override
              public void onTick(long millisUntilFinished) {
                  // every time 1 second passes`enter code here`
                  textTimeLeft.setText("Seconds Left: "+millisUntilFinished/1000);
              }
  
          }.start();
      }
  
  }
  </code></pre>
  
  <p>I can take a photo using this code but without default camera opened.
      How to take a photo after opening the camera without  using surfaceview.As i am doing all my designs in html css,no xml.
      please help me.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Those two answer was using surface view.In my case i need to open default camera app and has to take a photo. Like how selfie stick works.</p>
  
  <p>in simple:
  how to automatically  take photo from default camera app.</p>