开发者

Camera application giving exception at the start in android

To get started with camera application in android I have written some code to perform the following steps: 1.Preview

2.Capture Image

3.Save Image

4.Preview Again

Here is the whole code to perform the above steps:

public class CameraActivity extends Activity implements SurfaceHolder.Callback,Camera.AutoFocusCallback,Camera.PictureCallback
{
String SD_CARD_TEMP_DIR = Environment.getExternalStorageDirectory() + File.separator + "tmpPhoto.jpg";
private Camera mCamera;
boolean mPreviewRunning = false;
ImageButton takePictureButton;
SurfaceView mSurfaceView;
SurfaceHolder mSurfaceHolder;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getWindow().setFormat(PixelFormat.TRANSLUCENT);
    setContentView(R.layout.camera);

    mSurfaceView = (SurfaceView)findViewById(R.id.surface);
    takePictureButton = (ImageButton)findViewById(R.id.capture_img);
    takePictureButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            mCamera.takePicture(null, null, CameraActivity.this);
        }
    });
    mSurfaceHolder = mSurfaceView.getHolder();
    mSurfaceHolder.addCallback(this);
    mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

}
@Override
public void onPictureTaken(byte[] data, Camera camera) {
    // TODO Auto-generated method stub

    Intent takePictureFromCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    takePictureFromCameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(SD_CARD_TEMP_DIR)));
    startActivityForResult(takePictureFromCameraIntent, 1);

}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    //Picture taken from camera
    if(requestCode == 1)
    {
        if(resultCode == Activity.RESULT_OK)
        {
            File f = new File(SD_CARD_TEMP_DIR);
            try{
                Uri capturedImage = 
                    Uri.parse(android.provider.MediaStore.Images.Media.insertImage(getContentResolver(), f.getAbsolutePath(), 
                            null, null));

                Log.i("camera", "Selected image: "+capturedImage.toString());
                f.delete();
            }catch (FileNotFoundException e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
    }
    else
    {
        Log.i("camera", "Result Code was: " + resultCode);
    }
}
@Override
public void onAut开发者_运维百科oFocus(boolean success, Camera camera) {
    // TODO Auto-generated method stub
    camera.takePicture(null, null, this);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    // TODO Auto-generated method stub
    if(mPreviewRunning)
    {
        mCamera.stopPreview();
    }
    Camera.Parameters p = mCamera.getParameters();
    p.setPreviewSize(width, height);
    p.setPictureFormat(PixelFormat.JPEG);
    mCamera.setParameters(p);

    try
    {
        mCamera.setPreviewDisplay(holder);
    }catch(IOException ie)
    {
        ie.printStackTrace();
    }
    mCamera.startPreview();
    mPreviewRunning = true;
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    mCamera = Camera.open();

}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    mCamera.stopPreview();
    mPreviewRunning = false;
    mCamera.release();

}
 }

When I run the above code, at the start of the app itself it is giving me a message that it has stopped unexpectedly. Have I done any mistake here? I want to know because I am new for Camera. Please help me!!! Thank You


Have you include Camera permission in Manifest?

uses-permission android:name="android.permission.CAMERA"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜