title: native_takePicture() error when pressing button to take picture
body: <p>I've been trying to look for a similar error but I'm having a hard time... I'm still new to android development, but hopefully someone can shine some direction.</p>
  
  <p>I have a camera app that can preview, but when I tried to click on a button to take the picture, my app crashes. Can someone help me?</p>
  
  <p>[PhotoActivity.java]</p>
  
  <pre><code>public class PhotoActivity extends Activity {
      public static final int MEDIA_TYPE_IMAGE = 1;
  
      protected static final String TAG = "Activity";
      private Camera mCamera;
      private CameraPreview mCameraPreview;
  
  
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.photo);
  
          mCamera = getCameraInstant();
          mCameraPreview = new CameraPreview(this, mCamera);
  
          FrameLayout preview = (FrameLayout) findViewById(id.camera_preview);
          preview.addView(mCameraPreview);
  
          // Add a listener to the Capture button
          Button captureButton = (Button) findViewById(id.button_capture);
          captureButton.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      // get an image from the camera
                      Log.e("log", "mPicture"+mPicture);
  
                      mCamera.takePicture(null, null, mPicture);
                  }
              }
          );  
      }
  
      PictureCallback mPicture = new PictureCallback(){
       @Override
          public void onPictureTaken(byte[] data, Camera camera) {
              // TODO Auto-generated method stub
              File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
              if(pictureFile==null){
  
                  return;
              }
  
              try {
                  FileOutputStream fos = new FileOutputStream(pictureFile);
                  fos.write(data);
                  fos.close();
  
              } catch (FileNotFoundException e) {
                  Log.d(TAG, "File not found: " + e.getMessage());
  
              } catch (IOException e){
                  Log.d(TAG, "Error accessing file: " + e.getMessage());
  
              }
          }
  
      };
  
  
  
      /**
       * Helper method to access the camera returns null if
       * it cannot get the camera or does not exist
       * @return
       */
      private Camera getCameraInstant(){
          Camera camera = null;
  
          try{
              camera=Camera.open();
          }catch (Exception e){
              // cannot get camera or does not exist
          }
          return camera;
      }
  
      /** Create a File for saving the image */
      private File getOutputMediaFile(int type){
  
          File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_PICTURES), "MyCameraApp");
  
          if (! mediaStorageDir.exists()){
              if (! mediaStorageDir.mkdirs()){
                  Log.d("MyCameraApp", "failed to create directory");
                  return null;
              }
          }
  
          // Create a media file name
          String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
          File mediaFile;
              mediaFile = new File(mediaStorageDir.getPath() + File.separator +
              "IMG_"+ timeStamp + ".jpg");
  
          return mediaFile;
      }
  
  }
  </code></pre>
  
  <p>Sorry for all the coding, but I'm really in need of some help... Thanks in advance.</p>
  
  <p><strong>UPDATED</strong></p>
  
  <p>[CameraPreview.java]</p>
  
  <pre><code>public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
  
      private static final String TAG = "Preview";
      private SurfaceHolder mSurfaceHolder;
      private Camera mCamera;
  
      //Constructor that obtains context and camera
      public CameraPreview(Context context, Camera camera) {
          super(context);
          //this.mCamera = camera;
          this.mCamera = camera;
          this.mSurfaceHolder = this.getHolder();
          this.mSurfaceHolder.addCallback(this); // we get notified when underlying surface is created and destroyed
          this.mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); //this is a deprecated method, is not requierd after 3.0
      }
  
      @Override
      public void surfaceCreated(SurfaceHolder surfaceHolder) {
          mCamera.release();
          mCamera = Camera.open();
  
          try {
              mCamera.setPreviewDisplay(surfaceHolder);
  
              mCamera.setDisplayOrientation(90);
  
              mCamera.startPreview();
          } catch (IOException e) {
            // left blank for now
              Log.d(TAG, "Error setting camera preview: " + e.getMessage());
  
          }
  
      }
  
      @Override
      public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
          if (mCamera != null) {
              Log.d(TAG,"Stopping preview in SurfaceDestroyed().");
              mCamera.setPreviewCallback(null);
          mCamera.stopPreview();
          mCamera.release();
          }
      }
  
      @Override
      public void surfaceChanged(SurfaceHolder surfaceHolder, int format,
              int width, int height) {
  
  
          if(mSurfaceHolder.getSurface()==null){
              //preview surface does not exist
              return;
          }
          try {
              mCamera.stopPreview();
          }catch(Exception e){
              //ignore: tried to stop a non-existent preview
          }
  
          // start preview with new settings
          try {
              mCamera.setPreviewDisplay(mSurfaceHolder);
              mCamera.setDisplayOrientation(90);
              mCamera.startPreview();
          } catch (Exception e) {
              // intentionally left blank for a test
              Log.d(TAG, "Error starting camera preview: "+e.getMessage());
          }
      }
  
  }
  </code></pre>
  
  <p><strong>Error</strong></p>
  
  <pre><code>05-09 21:19:29.013: E/AndroidRuntime(3823): FATAL EXCEPTION: main
  05-09 21:19:29.013: E/AndroidRuntime(3823): java.lang.RuntimeException: Method called after release()
  05-09 20:21:01.214: E/AndroidRuntime(2813):     at android.hardware.Camera.native_takePicture(Native Method)
      05-09 20:21:01.214: E/AndroidRuntime(2813):     at android.hardware.Camera.takePicture(Camera.java:746)
      05-09 20:21:01.214: E/AndroidRuntime(2813):     at android.hardware.Camera.takePicture(Camera.java:710)
      05-09 20:21:01.214: E/AndroidRuntime(2813):     at com.liu.photo.PhotoActivity$2.onClick(PhotoActivity.java:73)
      05-09 20:21:01.214: E/AndroidRuntime(2813):     at android.view.View.performClick(View.java:2486)
      05-09 20:21:01.214: E/AndroidRuntime(2813):     at android.view.View$PerformClick.run(View.java:9130)
      05-09 20:21:01.214: E/AndroidRuntime(2813):     at android.os.Handler.handleCallback(Handler.java:587)
      05-09 20:21:01.214: E/AndroidRuntime(2813):     at android.os.Handler.dispatchMessage(Handler.java:92)
      05-09 20:21:01.214: E/AndroidRuntime(2813):     at android.os.Looper.loop(Looper.java:130)
      05-09 20:21:01.214: E/AndroidRuntime(2813):     at android.app.ActivityThread.main(ActivityThread.java:3703)
      05-09 20:21:01.214: E/AndroidRuntime(2813):     at java.lang.reflect.Method.invokeNative(Native Method)
      05-09 20:21:01.214: E/AndroidRuntime(2813):     at java.lang.reflect.Method.invoke(Method.java:507)
      05-09 20:21:01.214: E/AndroidRuntime(2813):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
      05-09 20:21:01.214: E/AndroidRuntime(2813):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
      05-09 20:21:01.214: E/AndroidRuntime(2813):     at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>Is this what the stack trace is? I'm very new, still learning the terms as well. Thank you</p>