相机请求权限带来错误

时间:2018-10-02 09:43:03

标签: java android

我是Android的新手,我正在尝试构建手电筒应用程序。我试图请求使用相机的权限,但相机无法解决CAMERA_REQUEST。任何解释和解决方案。在下面找到代码。

mButtonEnabled = findViewById(R.id.buttonEnable);
    mButtonEnabled.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ActivityCompat.requestPermissions(MainActivity.this,
                    new String[] {Manifest.permission.CAMERA}, CAMERA_REQUEST);
        }
    });
}

3 个答案:

答案 0 :(得分:0)

您可以使用任何数字作为CAMERA_REQUEST。 Android没有此属性,它只是您下次处理响应的标识符。

答案 1 :(得分:0)

接下来,我编写了用于授予Camera运行时权限的代码,其中有一个Array字符串,您可以在其中给出多个运行时所需的请求授予。

公共类MainActivity扩展了AppCompatActivity {     私有静态最终int PERMISSION_REQUEST_CODE = 200;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (checkPermission()) {
    //main logic or main code

   // . write your main code to execute, It will execute if the permission is already given.

    } else {
        requestPermission();
    }
}

private boolean checkPermission() {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
            != PackageManager.PERMISSION_GRANTED) {
        // Permission is not granted
        return false;
    }
    return true;
}

private void requestPermission() {

    ActivityCompat.requestPermissions(this,
            new String[]{Manifest.permission.CAMERA},
            PERMISSION_REQUEST_CODE);
}

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case PERMISSION_REQUEST_CODE:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(getApplicationContext(), "Permission Granted", Toast.LENGTH_SHORT).show();

                // main logic
            } else {
                Toast.makeText(getApplicationContext(), "Permission Denied", Toast.LENGTH_SHORT).show();
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
                            != PackageManager.PERMISSION_GRANTED) {
                        showMessageOKCancel("You need to allow access permissions",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                                            requestPermission();
                                        }
                                    }
                                });
                    }
                }
            }
            break;
    }
}

private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
    new AlertDialog.Builder(MainActivity.this)
            .setMessage(message)
            .setPositiveButton("OK", okListener)
            .setNegativeButton("Cancel", null)
            .create()
            .show();
}

}

希望对您有帮助

答案 2 :(得分:0)

我假设您得到的错误是编译器错误,因为没有CAMERA_REQUEST的声明。这只是您定义为所发出请求的ID的数字。理想情况下,应将其定义为实例变量。

此ID将通过方法调用作为requestCode传递回给您,以帮助您识别响应的请求。它还可以提供分别授予的权限,因为您可以一次请求多个权限:

private final int REQUEST_CAMERA_ID = 123456;

public void setupUI() {

    mButtonEnabled = findViewById(R.id.buttonEnable);
    mButtonEnabled.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ActivityCompat.requestPermissions(MainActivity.this,
                    new String[] {Manifest.permission.CAMERA}, REQUEST_CAMERA_ID);
        }
    });
}

public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {

    switch (requestCode) {
        case REQUEST_CAMERA_ID:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission was granted
            } else {
                    // Permission was denied
            }
        break;
    }
}