简体   繁体   English

相机错误:无法连接到相机

[英]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? 这是为什么?

Using camera with ACTION_IMAGE_CAPTURE intent with target API 23 or higher requires camera permission. 使用具有目标API 23或更高版本的ACTION_IMAGE_CAPTURE意向的相机需要相机许可。 You must request this permission at runtime. 您必须在运行时请求此权限。

Here you can find a video tutorial on this topic. 在这里,您可以找到有关此主题的视频教程。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM