[英]Camera error: Cannot connect to the camera
I want to use the built-in camera functionality of a device. 我想使用设备的内置摄像头功能。 having read the documentation, this is the method I am using
阅读文档后,这就是我使用的方法
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
In the manifest I added the usage: 在清单中,我添加了用法:
<uses-feature android:name="android.hardware.camera"
android:required="true" />
However, when I invoke the method, the error dialog appears. 但是,当我调用该方法时,将出现错误对话框。 Why is that?
这是为什么?
If you want to open the camera on button click, use the code below. 如果要在单击按钮时打开相机,请使用以下代码。
Initialize the button and image view within the onCreate() method. 在onCreate()方法中初始化按钮和图像视图。
photoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
if(photo!=null)
imageView.setImageBitmap(photo);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.