title: Camera not saving image and drops exception
body: <p>I'm creating camera app. I'm doing that in fragment. I have <code>FrameLayout</code> where I add the camera, and one button for capture. There are 2 problems.</p>
  
  <ol>
  <li><p>Camera not saves the image.</p></li>
  <li><p>When I click second time on capture image, drops exception <code>java.lang.RuntimeException: takePicture failed</code></p></li>
  </ol>
  
  <p>I don't know where's the problem, I'm using camera API first time.</p>
  
  <p>Camera I'm using only in portrait mode. And in manifests I added the permissions for camera and write external storage.</p>
  
  <p>I have one class where I'm extending <code>SurfaceView</code>. Let me show you the code.</p>
  
  <pre><code>public class ShowCamera extends SurfaceView implements SurfaceHolder.Callback{
  
  Camera camera;
  SurfaceHolder holder;
  
  public ShowCamera(Context context, Camera camera) {
      super(context);
      this.camera = camera;
      holder = getHolder();
      holder.addCallback(this);
  }
  
  @Override
  public void surfaceCreated(SurfaceHolder surfaceHolder) {
      Camera.Parameters params = camera.getParameters();
  
      List&lt;Camera.Size&gt; sizes = params.getSupportedPictureSizes();
      Camera.Size mSize = null;
  
      for (Camera.Size size : sizes){
          mSize = size;
      }
  
      camera.setDisplayOrientation(90);
      params.setRotation(90);
  
      params.setPictureSize(mSize.width, mSize.height);
      camera.setParameters(params);
      try {
          camera.setPreviewDisplay(surfaceHolder);
          camera.startPreview();
      } catch (IOException e) {
          e.printStackTrace();
      }
  }
  
  @Override
  public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
  
  }
  
  @Override
  public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
      camera.stopPreview();
      camera.release();
  }
  }
  </code></pre>
  
  <p>Ok and here's the fragment for camera.</p>
  
  <pre><code>public class CameraActivity extends BaseFragment{
  View mainView;
  FrameLayout cameraLayout;
  Camera camera;
  ShowCamera showCamera;
  ImageButton captureImage;
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      mainView = inflater.inflate(R.layout.camera_fragment, container, false);
  
      cameraLayout = (FrameLayout) mainView.findViewById(R.id.cameraContainer);
      captureImage = (ImageButton) mainView.findViewById(R.id.imageButton);
  
      camera = Camera.open();
      showCamera = new ShowCamera(getActivity(), camera);
  
      cameraLayout.addView(showCamera);
  
      capture();
  
      return mainView;
  }
  
  Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
      @Override
      public void onPictureTaken(byte[] bytes, Camera camera) {
          if(bytes != null){
              Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
              if(bitmap != null){
                  File file = new File(String.valueOf(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)));
                  if(!file.isDirectory()){
                      file.mkdir();
                  }
  
                  file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),
                          System.currentTimeMillis() + ".jpg");
  
                  try {
                      FileOutputStream fileOutputStream = new FileOutputStream(file);
                      bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
  
                      fileOutputStream.flush();
                      fileOutputStream.close();
  
                  } catch (Exception e){
                      e.printStackTrace();
                  }
              }
          }
      }
  };
  
  public void capture(){
      captureImage.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              if(camera != null){
                  camera.takePicture(null, null, mPictureCallback);
              }
          }
      });
  
  }
  }
  </code></pre>