title: Java.lang.Runtime exception: Take Picture failed?
body: <p>I am taking pictures in the background inside my Android App. However it gives an error: </p>
  
  <pre><code>02-09 15:22:12.061: E/cheeta(28633): timer testing
  02-09 15:22:13.546: W/System.err(28633): java.lang.RuntimeException: takePicture failed
  02-09 15:22:13.546: W/System.err(28633):    at android.hardware.Camera.native_takePicture(Native Method)
  02-09 15:22:13.546: W/System.err(28633):    at android.hardware.Camera.takePicture(Camera.java:1194)
  02-09 15:22:13.551: W/System.err(28633):    at cam.sharp.MainActivity$MyTimerTask.run(MainActivity.java:69)
  02-09 15:22:13.551: W/System.err(28633):    at java.util.Timer$TimerImpl.run(Timer.java:284)
  02-09 15:22:13.551: E/cheeta(28633): timer testing
  02-09 15:22:15.051: W/System.err(28633): java.lang.RuntimeException: takePicture failed
  02-09 15:22:15.051: W/System.err(28633):    at android.hardware.Camera.native_takePicture(Native Method)
  02-09 15:22:15.051: W/System.err(28633):    at android.hardware.Camera.takePicture(Camera.java:1194)
  02-09 15:22:15.051: W/System.err(28633):    at cam.sharp.MainActivity$MyTimerTask.run(MainActivity.java:69)
  02-09 15:22:15.051: W/System.err(28633):    at java.util.Timer$TimerImpl.run(Timer.java:284)
  02-09 15:22:15.051: E/cheeta(28633): timer testing
  02-09 15:22:16.551: W/System.err(28633): java.lang.RuntimeException: takePicture failed
  02-09 15:22:16.556: W/System.err(28633):    at android.hardware.Camera.native_takePicture(Native Method)
  02-09 15:22:16.556: W/System.err(28633):    at android.hardware.Camera.takePicture(Camera.java:1194)
  02-09 15:22:16.561: W/System.err(28633):    at cam.sharp.MainActivity$MyTimerTask.run(MainActivity.java:69)
  02-09 15:22:16.561: W/System.err(28633):    at java.util.Timer$TimerImpl.run(Timer.java:284)
  02-09 15:22:16.561: E/cheeta(28633): timer testing
  </code></pre>
  
  <p>I have two files.</p>
  
  <p>MainActivity.java and CameraPreview.java</p>
  
  <p>Here is the code for both.</p>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>package cam.sharp;
  
  import java.io.File;
  import java.io.FileOutputStream;
  import java.util.Timer;
  import java.util.TimerTask;
  
  import android.app.Activity;
  import android.content.Context;
  import android.hardware.Camera;
  import android.os.Build;
  import android.os.Bundle;
  import android.util.Log;
  import android.widget.FrameLayout;
  import android.widget.Toast;
  
  public class MainActivity extends Activity {
  
      private int cameraId = 0;
      private Camera mCamera;
      private CameraPreview mPreview;
      String fileName = "tempImage.jpeg";
      File file;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          // Create an instance of Camera
          mCamera = getCameraInstance(cameraId);
  
          if (mCamera == null) {
              Toast.makeText(
                      getApplicationContext(),
                      "The camera service is currently unavailable, please try again!",
                      Toast.LENGTH_LONG).show();
              finish();
          } else {
              // Create our Preview view and set it as the content of our
              // activity.
              mPreview = new CameraPreview(this, mCamera);
              FrameLayout frameLayout = (FrameLayout) findViewById(R.id.camera_preview);
              frameLayout.addView(mPreview);
  
          }
  
          // start thread for these
  
          MyTimerTask myTask = new MyTimerTask();
          Timer myTimer = new Timer();
          // public void schedule (TimerTask task, long delay, long period)
          // Schedule a task for repeated fixed-delay execution after a specific
          // delay.
          //
          // Parameters
          // task the task to schedule.
          // delay amount of time in milliseconds before first execution.
          // period amount of time in milliseconds between subsequent executions.
  
          myTimer.schedule(myTask, 3000, 1500);
  
      }
  
      class MyTimerTask extends TimerTask {
          public void run() {
  
              try {
                  mCamera.takePicture(null, null, null, mPictureCallback);
                  file = new File(getFilesDir(), fileName);
  
              } catch (Exception e) {
                  e.printStackTrace();
              }
  
              Log.e("cheeta", "timer testing");
  
          }
      }
  
      Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
          public void onPictureTaken(byte[] imageData, Camera c) {
              Log.e("Callback TAG", "Here in jpeg Callback");
  
              if (imageData != null) {
                  FileOutputStream outputStream;
                  try {
                      outputStream = openFileOutput(fileName,
                              Context.MODE_PRIVATE);
                      outputStream.write(imageData);
                      outputStream.close();
  
                      // Intent intent = new Intent(SnapScreen.this,
                      // PreviewScreen.class);
                      // if (fromMessageReview == true) {
                      // intent.putExtra("fromMessageReview", "true");
                      // }
                      // startActivity(intent);
                      // overridePendingTransition(R.anim.slide_in,
                      // R.anim.slide_out);
  
                      finish();
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
  
              }
          }
      };
  
      @Override
      protected void onDestroy() {
          super.onDestroy();
          releaseCamera();
      }
  
      /** A safe way to get an instance of the Camera object. */
      public static Camera getCameraInstance(int cameraId) {
          Camera c = null;
          try {
              if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.GINGERBREAD) {
                  c = Camera.open(cameraId);
              } else {
                  c = Camera.open();
              }
          } catch (Exception e) {
              c = null;
          }
          return c; // returns null if camera is unavailable
      }
  
      private void releaseCamera() {
          if (mCamera != null) {
              mCamera.release(); // release the camera for other applications
              mCamera = null;
          }
      }
  
  }
  </code></pre>
  
  <p><strong>CameraPreview.java</strong></p>
  
  <pre><code>package cam.sharp;
  
  import java.io.IOException;
  
  import android.annotation.SuppressLint;
  import android.content.Context;
  import android.hardware.Camera;
  import android.util.Log;
  import android.view.SurfaceHolder;
  import android.view.SurfaceView;
  
  /** A basic Camera preview class */
  @SuppressLint("ViewConstructor")
  public class CameraPreview extends SurfaceView implements
  SurfaceHolder.Callback {
      private static final String TAG = "Camera Preview";
      private SurfaceHolder mHolder;
      public Camera mCamera;
  
      @SuppressWarnings("deprecation")
      @SuppressLint("NewApi")
      public CameraPreview(Context context, Camera camera) {
          super(context);
          mCamera = camera;
          mCamera.setDisplayOrientation(90);
  
          // Install a SurfaceHolder.Callback so we get notified when the
          // underlying surface is created and destroyed.
          mHolder = getHolder();
          mHolder.addCallback(this);
          // deprecated setting, but required on Android versions prior to 3.0
          mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
      }
  
      public void surfaceCreated(SurfaceHolder holder) {
          // The Surface has been created, now tell the camera where to draw the
          // preview.
          try {
                  mCamera.setPreviewDisplay(holder);
                  mCamera.setDisplayOrientation(90);
                  mCamera.startPreview(); 
  
          } catch (IOException e) {
              Log.d(TAG, "Error setting camera preview: " + e.getMessage());
          }
      }
  
      public void surfaceDestroyed(SurfaceHolder holder) {
          // empty. Take care of releasing the Camera preview in your activity.
      }
  
      public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
          // If your preview can change or rotate, take care of those events here.
          // Make sure to stop the preview before resizing or reformatting it.
          if (mHolder.getSurface() == null) {
              // preview surface does not exist
              return;
          }
  
          // stop preview before making changes
          try {
              mCamera.stopPreview();
          } catch (Exception e) {
              // ignore: tried to stop a non-existent preview
          }
  
          // set preview size and make any resize, rotate or
          // reformatting changes here
  
          // start preview with new settings
          try {
              mCamera.setPreviewDisplay(mHolder);
              mCamera.startPreview();
          } catch (Exception e) {
              Log.d(TAG, "Error starting camera preview: " + e.getMessage());
          }
      }
  }
  </code></pre>
  
  <p>Can anyone see what is the issue? I am calling <strong>mCamera.startPreview();</strong> but still no use.</p>
  
  <p>Thanks</p>