title: How to take a photo using CameraSource?
body: <p>I'm having some problems with CameraSource when I want to take a picture using the camera.
  All i want is take a picture with a button i made on the xml file and use 'CameraSource' to take the photo because i'm using 'TextRecognizer' too.
  The Camera is working fine but the problem occurs when i press the button to take a picture.</p>
  
  <p>Manifest File:</p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.CAMERA" /&gt;
  &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
  &lt;uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /&gt;
  </code></pre>
  
  <p>My Variables:</p>
  
  <pre><code>SurfaceView cameraView;
  TextView textView;
  CameraSource camera_source;
  final int RequestCameraPermissionID = 1001;
  </code></pre>
  
  <p>Class:</p>
  
  <pre><code>// Starting the CameraSource. This will be in the 'OnCreate' function. This function works.
  private void OpenCameraSource(){
          final TextRecognizer textRecognizer = new TextRecognizer.Builder(getApplicationContext()).build();
          if (!textRecognizer.isOperational()) {
              Log.w("Steal_Activity", "Detector activity are not yet available");
              Toast.makeText(getApplicationContext(),"Detector activity are not yet available",Toast.LENGTH_SHORT).show();
          } else {
              camera_source = new CameraSource.Builder(getApplicationContext(),textRecognizer)
                      .setFacing(CameraSource.CAMERA_FACING_BACK)
                      .setRequestedPreviewSize(1280, 1024)
                      .setRequestedFps(2.0f)
                      .setAutoFocusEnabled(true)
                      .build();
              cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
                  @Override
                  public void surfaceCreated(SurfaceHolder surfaceHolder) {
                      try {
                          if (ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                              ActivityCompat.requestPermissions(Steal.this,
                                      new String[]{android.Manifest.permission.CAMERA},
                                      RequestCameraPermissionID);
                              return;
                          }
                          camera_source.start(cameraView.getHolder());
                      }catch (IOException ex){
                          ex.printStackTrace();
                      }
                  }
                  @Override
                  public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
                  }
                  @Override
                  public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
                      camera_source.stop();
                  }
              });
              textRecognizer.setProcessor(new Detector.Processor&lt;TextBlock&gt;() {
                  // Some Stuff here
              }
          });
      }
  }
  
  
  // This function will be launched when the button is pressed
  public void OnClick_TakeShot(View view){
          try{//I GET ERROR HERE
              camera_source.takePicture(null,pictureCallback);
          }catch (Exception ex){
              Toast.makeText(getApplicationContext(),"Error:"+ex.toString(),Toast.LENGTH_LONG).show();
          }
  }
  
  // Callback for 'takePicture'
  CameraSource.PictureCallback pictureCallback = new CameraSource.PictureCallback() {
          @Override
          public void onPictureTaken(byte[] bytes) {
              File file_image = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES+"/pics");
              Bitmap bitmap = BitmapFactory.decodeByteArray(bytes , 0, bytes .length);
              if(bitmap!=null){
                  if(!file_image.isDirectory()){
                      file_image.mkdir();
                  }
                  file_image=new File(file_image,"mylastpic.jpg");
                  try{
                      FileOutputStream fileOutputStream=new FileOutputStream(file_image);
                      bitmap.compress(Bitmap.CompressFormat.JPEG,100, fileOutputStream);
                      fileOutputStream.flush();
                      fileOutputStream.close();
                  }
                  catch(Exception exception) {
                      Toast.makeText(getApplicationContext(),"Error saving: "+ exception.toString(),Toast.LENGTH_LONG).show();
                  }
              }
          }
  };
  </code></pre>
  
  <p><strong>NOT WORKING WHEN I PRESS THE BUTTON TO TAKE PICTURE</strong>
  <a href="https://i.stack.imgur.com/ToiAO.png" rel="noreferrer"><img src="https://i.stack.imgur.com/ToiAO.png" alt="enter image description here"></a></p>