相机应用程序在android开始时给出异常

时间:2011-08-04 06:50:17

标签: android camera

要开始使用android中的相机应用程序,我已编写了一些代码来执行以下步骤: 1.预览
2.捕获图像
3.保存图像
4.再次预览

以下是执行上述步骤的完整代码:

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 onAutoFocus(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();

}
 }

当我运行上面的代码时,在应用程序本身的开头,它会给我一条消息,表明它已经意外停止了。 我在这做错了吗?我想知道,因为我是Camera的新手。请帮我!!! 谢谢

1 个答案:

答案 0 :(得分:0)

您是否在Manifest中包含了相机权限?

uses-permission android:name =“android.permission.CAMERA”