title: Open camera directly in the Activity without clicking on button and without intent?
body: <p>I am trying to open my Camera without clicking on my button but it doesn't work...</p>
  
  <p>I follow instructions here : <a href="http://developer.android.com/guide/topics/media/camera.html#custom-camera" rel="nofollow noreferrer">http://developer.android.com/guide/topics/media/camera.html#custom-camera</a></p>
  
  <p>Here is my result :</p>
  
  <p><img src="https://i.stack.imgur.com/sHkF0.png" alt="enter image description here"></p>
  
  <p>When i press the button "Capture", it activates the camera but i want to activate the camera before this and just record when i click on the button like snapchat. I think i miss somewhere when i open my camera but i don't find my mistakes...</p>
  
  <p>Here is my class :</p>
  
  <pre><code>    @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      preview = (FrameLayout) findViewById(R.id.camera_preview);
  
  
      if (checkCameraHardware(this)) {
          mCamera = getCameraInstance();
          mPreview = new CameraPreview(this, mCamera);
          mCamera.setDisplayOrientation(90);
  
          initButton();
  
          preview.addView(mPreview);
          preview.removeAllViews();
          preview.addView(mPreview);
          preview.addView(captureButton);
      }
  }
  </code></pre>
  
  <p>After that, i have the method which initiate the button :</p>
  
  <pre><code>public void initButton () {
      captureButton = (Button) findViewById(R.id.button_capture);
  
      captureButton.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              if (isRecording) {
                  // stop recording and release camera
                  mMediaRecorder.stop();
                  releaseMediaRecorder();
                  mCamera.lock();
  
                  // inform the user that recording has stopped
                  captureButton.setText("Capture");
                  isRecording = false;
              } else {
                  // initialize video camera
                  if (prepareVideoRecorder()) {
                      // Camera is available and unlocked, MediaRecorder is prepared,
                      // now you can start recording
                      mMediaRecorder.start();
  
                      captureButton.setText("Stop");
                      isRecording = true;
                  } else {
                      releaseMediaRecorder();
                      Toast.makeText(MainActivity.this, "Camera doesn't work", Toast.LENGTH_LONG).show();
                  }
              }
          }
      });
  
  }
  </code></pre>
  
  <p>Here is my method which get instance of the Camera :</p>
  
  <pre><code>public static Camera getCameraInstance() {
      Camera c = null;
      try {
          c = Camera.open();
      } catch (Exception e) {
          Log.i("CAMERA INFO : &gt;", "Camera doesn't exist");
      }
      return c;
  }
  </code></pre>
  
  <p>Here is the method to prepare the record : </p>
  
  <pre><code>private boolean prepareVideoRecorder() {
      mMediaRecorder = new MediaRecorder();
  
      // Step 1: Unlock and set camera to MediaRecorder
      mCamera.unlock();
      mMediaRecorder.setCamera(mCamera);
  
      // Step 2: Set sources
      mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
      mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
  
      // Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
      CamcorderProfile camcorderProfile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
  
      mMediaRecorder.setProfile(camcorderProfile);
  
      // Step 4: Set output file
      mMediaRecorder.setOutputFile(getOutputMediaFile(CAPTURE_VIDEO_FILE).toString());
  
      // Step 5: Set the preview output
      mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());
  
      // Step 6: Prepare configured MediaRecorder
      try {
          mMediaRecorder.prepare();
      } catch (IllegalStateException e) {
          Log.d("PREPARE MEDIARECORDER : &gt;", ": &gt; &gt; IllegalStateException preparing MediaRecorder: " + e.getMessage());
          releaseMediaRecorder();
          return false;
      } catch (IOException e) {
          Log.d("PREPARE MEDIARECORDER : &gt;", " : &gt; &gt; IOException preparing MediaRecorder: " + e.getMessage());
          releaseMediaRecorder();
          return false;
      }
      return true;
  }
  </code></pre>
  
  <p>then i try to open camera before the click with the instantiation but i don't know how... If some one has an idea, will be useful for me.</p>