简体   繁体   English

Android Studio 2-相机SecurityException

[英]Android Studio 2 - Camera SecurityException

I'm trying to use camera intent to capture a photo and display in an ImageView. 我正在尝试使用相机意图捕获照片并在ImageView中显示。

Any time the camera intent is started, the following exception occurs: 每当启动摄像机意图时,就会发生以下异常:

    java.lang.SecurityException: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE cmp=com.android.camera/.Camera } from ProcessRecord{c4afeea 3037:com.example.jt.testapp/u0a57} (pid=3037, uid=10057) with revoked permission android.permission.CAMERA

Calling code: 调用代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button gallery = (Button) findViewById(R.id.btnGallery);
    gallery.setOnClickListener(new Gallery(this));

    Button camera = (Button) findViewById(R.id.btnCamera);
    camera.setOnClickListener(new Camera(this));

}

What is the correct manifest permission to avoid receiving the above exception? 避免收到上述异常的正确清单权限是什么?

You are using the right permission: android.permission.CAMERA 您正在使用正确的权限:android.permission.CAMERA

When you want access to the Camera permission you have to ask it at runtime when running on Marshmallow. 如果要访问“相机”权限,则必须在运行于棉花糖上的运行时询问。

First check whether the permission is granted and when that isn't the case then request the permission: 首先检查是否授予了许可,如果不是,则请求许可:

if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.CAMERA)
    != PackageManager.PERMISSION_GRANTED) {
  ActivityCompat.requestPermissions(thisActivity,
            new String[]{Manifest.permission.CAMERA},
            0);
}

This is explained here in detail in the documentation: http://developer.android.com/training/permissions/requesting.html 在文档中对此进行了详细说明: http : //developer.android.com/training/permissions/requesting.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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