Camera force close when returning from sleep. Android
<p>In my app I'm capturing an image. Everything is working fine unless the phone goes to sleep while the preview is running. Not really sure how to handle it, I'm thinking that it may be best just to prevent the phone from automatically going to sleep while this process is in action. Which i do not know how to do. and if i do that will it prevent the phone from sleeping when i press the power button? perhaps there is a deeper issue here. </p>

<p>LogCat:</p>

<pre>

08-28 16:17:10.879: WARN/dalvikvm(9652): threadid=3: thread exiting with uncaught exception (group=0x4001b390)
08-28 16:17:10.879: ERROR/AndroidRuntime(9652): Uncaught handler: thread main exiting due to uncaught exception
08-28 16:17:10.879: ERROR/AndroidRuntime(9652): java.lang.RuntimeException: Method called after release()
08-28 16:17:10.879: ERROR/AndroidRuntime(9652):     at android.hardware.Camera.setHasPreviewCallback(Native Method)
08-28 16:17:10.879: ERROR/AndroidRuntime(9652):     at android.hardware.Camera.access$600(Camera.java:58)
08-28 16:17:10.879: ERROR/AndroidRuntime(9652):     at android.hardware.Camera$EventHandler.handleMessage(Camera.java:339)
08-28 16:17:10.879: ERROR/AndroidRuntime(9652):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-28 16:17:10.879: ERROR/AndroidRuntime(9652):     at android.os.Looper.loop(Looper.java:123)
08-28 16:17:10.879: ERROR/AndroidRuntime(9652):     at android.app.ActivityThread.main(ActivityThread.java:4595)
08-28 16:17:10.879: ERROR/AndroidRuntime(9652):     at java.lang.reflect.Method.invokeNative(Native Method)
08-28 16:17:10.879: ERROR/AndroidRuntime(9652):     at java.lang.reflect.Method.invoke(Method.java:521)
08-28 16:17:10.879: ERROR/AndroidRuntime(9652):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-28 16:17:10.879: ERROR/AndroidRuntime(9652):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-28 16:17:10.879: ERROR/AndroidRuntime(9652):     at dalvik.system.NativeStart.main(Native Method)
</pre>

<p>takephoto activity:</p>

<pre><code>public class takephoto extends Activity {
     private static final String TAG = "GrowJournalDemo";
      Preview preview; // &lt;1&gt;
      Button buttonClick; // &lt;2&gt;
      String journ_id;
      String plant_id;
    // private String plantid = ((resource) this.getApplication()).getplantId();
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.photo);

        preview = new Preview(this); // &lt;3&gt;
        ((FrameLayout) findViewById(R.id.preview)).addView(preview); // &lt;4&gt;
        String journalid = ((resource) this.getApplication()).getjournalName();
        plant_id = ((resource) this.getApplication()).getplantId();
        journ_id = journalid;
        buttonClick = (Button) findViewById(R.id.buttonClick);
        buttonClick.setOnClickListener(new OnClickListener() {
          public void onClick(View v) { // &lt;5&gt;

             // preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);
              preview.camera.autoFocus(new Camera.AutoFocusCallback() {
                  Camera.ShutterCallback shutterCallback = new Camera.ShutterCallback() {
                    public void onShutter() {
                      // Play your sound here.
                    }
                  };
                  public void onAutoFocus(boolean success, Camera camera) {
                    preview.camera.takePicture(null, null, jpegCallback);
                  }
                });

          }
        });

        Log.d(TAG, "onCreate'd");
      }

      // Called when shutter is opened
      ShutterCallback shutterCallback = new ShutterCallback() { // &lt;6&gt;
        public void onShutter() {
          Log.d(TAG, "onShutter'd");
        }
      };

      // Handles data for raw picture
      PictureCallback rawCallback = new PictureCallback() { // &lt;7&gt;
        public void onPictureTaken(byte[] data, Camera camera) {
          Log.d(TAG, "onPictureTaken - raw");
        }
      };

      // Handles data for jpeg picture
      PictureCallback jpegCallback = new PictureCallback() { // &lt;8&gt;
        public void onPictureTaken(byte[] data, Camera camera) {
          FileOutputStream outStream = null;
          try {
              android.os.Environment.getExternalStorageState();
            // create a File object for the parent directory
              File PhotoDirectory = new File(
                      android.os.Environment.getExternalStorageDirectory()+
                      "/GrowJournalPhotos/"+journ_id+"/"+plant_id+"/");
              // have the object build the directory structure, if needed.
              PhotoDirectory.mkdirs();
              // create a File object for the output file
              File outputFile = new File(PhotoDirectory, "photo.jpg");
              // now attach the OutputStream to the file object, instead of a String representation
              outStream = new FileOutputStream(outputFile);

            outStream.write(data);
            outStream.close();
            Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
            setResult(RESULT_OK);
            finish();

          } catch (FileNotFoundException e) { // &lt;10&gt;
            e.printStackTrace();
          } catch (IOException e) {
            e.printStackTrace();
          } finally {
          }
          Log.d(TAG, "onPictureTaken - jpeg");
        }
      };

    }
</code></pre>

<p>Preview surfaceview:</p>

<pre><code>public class Preview extends SurfaceView implements SurfaceHolder.Callback {
    private static final String TAG = "Preview";

      SurfaceHolder mHolder;  // &lt;2&gt;
      public Camera camera; // &lt;3&gt;

      Preview(Context context) {
        super(context);

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();  // &lt;4&gt;
        mHolder.addCallback(this);  // &lt;5&gt;
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); // &lt;6&gt;
      }

      // Called once the holder is ready
      public void surfaceCreated(SurfaceHolder holder) {  // &lt;7&gt;
        // The Surface has been created, acquire the camera and tell it where
        // to draw.
        camera = Camera.open(); // &lt;8&gt;
        try {
          camera.setPreviewDisplay(holder);  // &lt;9&gt;

          camera.setPreviewCallback(new PreviewCallback() { // &lt;10&gt;
            // Called for each frame previewed
            public void onPreviewFrame(byte[] data, Camera camera) {  // &lt;11&gt;
              Log.d(TAG, "onPreviewFrame called at: " + System.currentTimeMillis());
              Preview.this.invalidate();  // &lt;12&gt;
            }
          });
        } catch (IOException e) { // &lt;13&gt;
          e.printStackTrace();
        }
      }

      // Called when the holder is destroyed
      public void surfaceDestroyed(SurfaceHolder holder) {  // &lt;14&gt;
        camera.stopPreview();
        camera.release();
        camera = null;
      }

      // Called when holder has changed
      public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { // &lt;15&gt;
        camera.startPreview();
      }


    }
</code></pre>

<p>I've added a wake lock to prevent the phone from dimming, I feel this is a temporary solution though as there is still a force close when the device is forced to sleep via power button.</p>
