title: Android Camera - Sometimes when i take photos, the app freezes and the camera is non-usable
body: <p>I have built an application which takes photos when you touch the preview.
  I can take many photos, but sometimes when i touch the preview to take a photo, there is no shutter sound and the whole application freezes. Moreover, after that, if i try to launch launch the built-in camera application, i get a message that the camera can't be used.</p>
  
  <p>I don't know the reason for that behavior, it happens randomly and when it happens i must restart the device (Samsung Galaxy S) to be able to use the camera again.</p>
  
  <p>In the DDM, after the crash i can see the following line: keyDispatchingTimedOut</p>
  
  <p>Here is the relevant code:
  CameraActivity Class:</p>
  
  <pre><code>public class CameraActivity extends Activity {
    private static final String TAG = "CameraDemo";
    Preview preview;
  
    public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
  
      preview = new Preview(this);
      ((FrameLayout) findViewById(R.id.preview)).addView(preview);
      ((FrameLayout) findViewById(R.id.preview)).setOnTouchListener(preview); 
  
      Log.d(TAG, "Camera Activity Created.");
  
    }
  }
  </code></pre>
  
  <p>Preview Class:</p>
  
  <pre><code>    class Preview extends SurfaceView implements SurfaceHolder.Callback, OnTouchListener {
      private static final String TAG = "Preview";
  
      SurfaceHolder mHolder;
      public Camera camera;
      Context ctx;
      boolean previewing = false;
  
      Preview(Context context) {
          super(context);
          ctx = context;
          // Install a SurfaceHolder.Callback so we get notified when the
          // underlying surface is created and destroyed.
          mHolder = getHolder();
          mHolder.addCallback(this);
          mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
      }
  
  
      // Called once the holder is ready
      public void surfaceCreated(SurfaceHolder holder) {
          // The Surface has been created, acquire the camera and tell it where
          // to draw.
          camera = Camera.open();
      }
  
      // Called when the holder is destroyed
      public void surfaceDestroyed(SurfaceHolder holder) {
  
          if (camera != null) {
              camera.setPreviewCallback(null);
              camera.stopPreview();  
              camera.release();
              camera = null;
          }
  
          previewing = false;
      }
  
      // Called when holder has changed
      public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
  
          if(previewing){
               camera.stopPreview();
               previewing = false;
          }
  
          if (camera != null){
              try {
  
                  camera.setDisplayOrientation(90);
                  camera.setPreviewDisplay(holder);
                  camera.setPreviewCallback(new PreviewCallback() {
                      // Called for each frame previewed
                      public void onPreviewFrame(byte[] data, Camera camera) {
                          Log.d(TAG, "onPreviewFrame called at: " + System.currentTimeMillis());  
                          Preview.this.invalidate();
                      }
                  });
                  camera.startPreview();
                  previewing = true;
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
      }
  
      public boolean onTouch(View v, MotionEvent event) {
          camera.takePicture(shutterCallback, rawCallback, jpegCallback);
          return false;
      }
  
  
      // Called when shutter is opened
      ShutterCallback shutterCallback = new ShutterCallback() {
          public void onShutter() {
              Log.d(TAG, "onShutter'd");
          }
      };
  
      // Handles data for raw picture
      PictureCallback rawCallback = new PictureCallback() {
          public void onPictureTaken(byte[] data, Camera camera) {
              Log.d(TAG, "onPictureTaken - raw");
          }
      };
  
      // Handles data for jpeg picture
      PictureCallback jpegCallback = new PictureCallback() {
  
          public void onPictureTaken(byte[] data, Camera camera) {
              FileOutputStream outStream = null;
              try {
                  // Write to SD Card
                  outStream = new FileOutputStream(String.format("/sdcard/TVguide/Detection/detected.jpg", System.currentTimeMillis())); // &lt;9&gt;
                  outStream.write(data);
                  outStream.close();
                  Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
              } catch (FileNotFoundException e) { // &lt;10&gt;
                  //Toast.makeText(ctx, "Exception #2", Toast.LENGTH_LONG).show();
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();
              } finally {}
              Log.d(TAG, "onPictureTaken - jpeg");
              Toast.makeText(ctx, "SAVED", Toast.LENGTH_SHORT).show();
  
              camera.startPreview();
          }
      };
  
  }
  </code></pre>
  
  <p>Please help, i am trying a few days to understand where the problem is with no success</p>
  
  <p>Eyal</p>