title: Camera application in Android
body: <p>I have been searching some code for the camera application and got one but when I complete the code it is giving me some errors. Code any one please rectify my mistakes in the code.</p>
  
  <p>My code for Application is as follows </p>
  
  <pre><code>package abc.bae.camer;
  
  import java.io.FileNotFoundException;
  import java.io.FileOutputStream;
  import java.io.IOException;
  
  import android.app.Activity;
  import android.graphics.Camera;
  import android.hardware.Camera.PictureCallback;
  import android.hardware.Camera.ShutterCallback;
  import android.os.Bundle;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.FrameLayout;
  
  public class Bharath_cameraActivity extends Activity {
      private static final String TAG = "Bharath_cameraActivity";
      public Camera camera;
      public Preview preview;
      public Button buttonClick;
      public FrameLayout fl;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          preview = new Preview(this);
          fl =(FrameLayout)findViewById(R.id.preview).addView(preview);
          buttonClick = (Button) findViewById(R.id.click);
          buttonClick.setOnClickListener(new OnClickListener() {
          public void onClick(View v) {
                  preview.camera.takePicture(null,null, null);
              }
          });
      }
  
      ShutterCallback shutterCallback = new ShutterCallback() {
          public void onShutter() {
          }
      };
      PictureCallback rawCallback = new PictureCallback() {
          public void onPictureTaken(byte[] data, android.hardware.Camera camera) {
              // TODO Auto-generated method stub
          }
      };
      PictureCallback jpegCallback = new PictureCallback() {
          public void onPictureTaken(byte[] data, Camera camera) {
              FileOutputStream outStream = null;
              try {
                  outStream = new FileOutputStream(String.format(
                          "/sdcard/%d.jpg", System.currentTimeMillis()));
                  outStream.write(data);
                  outStream.close();
              } catch (FileNotFoundException e) {
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
  
          public void onPictureTaken(byte[] data, android.hardware.Camera camera) {
              // TODO Auto-generated method stub
          }
      };
  }
  </code></pre>
  
  <p>Code for the Preview.java is</p>
  
  <pre><code>package abc.bae.camer;
  
  import java.io.FileNotFoundException;
  import java.io.FileOutputStream;
  import java.io.IOException;
  
  import android.content.Context;
  import android.graphics.Camera;
  import android.graphics.Canvas;
  import android.graphics.Color;
  import android.graphics.Paint;
  import android.hardware.Camera.PreviewCallback;
  import android.view.SurfaceHolder;
  
  class Preview extends Bharath_cameraActivity implements SurfaceHolder.Callback {
      private static final String TAG = "Preview";
      SurfaceHolder mHolder;
      public Camera camera;
  
      Preview(Context context) {
          mHolder = getHolder();
          mHolder.addCallback(this);
          mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
      }
  
      public void surfaceCreated(SurfaceHolder holder) {
          camera = Camera.open();
          try {
              camera.setPreviewDisplay(holder);
              camera.setPreviewCallback(new PreviewCallback() {
                  public void onPreviewFrame(byte[] data, Camera arg1) {
                      FileOutputStream outStream = null;
                      try {
                          outStream = new FileOutputStream(String.format(
                                  "/sdcard/%d.jpg", System.currentTimeMillis()));
                          outStream.write(data);
                          outStream.close();
                      } catch (FileNotFoundException e) {
                          e.printStackTrace();
                      } catch (IOException e) {
                          e.printStackTrace();
                      }
                      Preview.this.invalidate();
                  }
              });
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  
      public void surfaceDestroyed(SurfaceHolder holder) {
          camera.stopPreview();
          camera = null;
      }
  
      public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
          Camera.Parameters parameters = camera.getParameters();
          parameters.setPreviewSize(w, h);
          camera.setParameters(parameters);
          camera.startPreview();
      }
  
      @Override
      public void draw(Canvas canvas) {
          super.draw(canvas);
          Paint p = new Paint(Color.RED);
          canvas.drawText("PREVIEW", canvas.getWidth() / 2,
                  canvas.getHeight() / 2, p);
      }
  }
  </code></pre>