Stack Exchange
Camera application in Android
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.
My code for Application is as follows 
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 for the Preview.java is
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);
    }
}


It is easier to help you if you post the errors that you are getting as well.

I have some code, it may help you:
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 1888);

and you have to also override onActivityResult() method to complete the process
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1888){
            if (resultCode == Activity.RESULT_OK) {
                bitmap = (Bitmap) data.getExtras().get("data");
                //show the bitmapImage
            }
        }
    }
}


here is my simple take a photo code hope this help
    private static final int TAKE_PHOTO_CODE = 1;

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);     
        takePhoto();
    }

...
    private void takePhoto(){
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);    
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile()));    
    startActivityForResult(intent,TAKE_PHOTO_CODE);
}

..
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {     
    if(requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK){

        // do stub like crop, rename , rotate pictue
    }
    else finish();          
}

..
private File getTempFile(){
  File root = new File(Environment.getExternalStorageDirectory(),"picture");
  if(!root.exists()){
    root.mkdirs();
  }
  File file = new File(root,"picture.jpg");
  return file;
}

