[英]Android Camera API takePicture() fails
The code below is my Android Camera API implementation, and as you can see i commented out the button/onClickListener bit. 下面的代码是我的Android Camera API实现,如您所见,我注释掉了button / onClickListener位。 This results in the error 'takePicture Failed' during runtime. 这会在运行时导致错误“ takePicture Failed”。 But when I use the button (and really nothing else) it works fine. 但是当我使用按钮时(实际上什么也没有),它都可以正常工作。 (shows preview, button works, image is saved to where I want it). (显示预览,按钮有效,图像保存到我想要的位置)。 However I would really like to do this without a button (take picture as soon as the activity is launched). 但是,我真的很想在没有按钮的情况下进行操作(活动启动后立即拍照)。 What's going wrong here? 这是怎么了
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_front_camera);
if(checkCameraHardware(this)) {
mCamera = getCameraInstance();
mPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);
mCamera.takePicture(null, null, mPicture);
mCamera.release();
// Add a listener to the Capture button
/*Button captureButton = (Button) findViewById(R.id.button_capture);
captureButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
// get an image from the camera
mCamera.takePicture(null, null, mPicture);
mCamera.release();
}
}
);
*/
//mCamera.takePicture(null, null, mPicture);
//mCamera.release();
}
}
您可以尝试覆盖活动生命周期函数onResume(),如果有帮助,可以在其中编写用于拍照的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.