无法在checkSelfPermission方法中解析符号'CAMERA'

时间:2016-04-01 06:09:17

标签: android android-camera android-permissions android-6.0-marshmallow runtime-permissions

I need to ask for runtime permission to use camera in my fragment class.my code is like this..


int currentapiVersion = android.os.Build.VERSION.SDK_INT;

if (currentapiVersion >= Build.VERSION_CODES.M) {

                if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {

                    requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA}, REQUEST_CODE_ASK_PERMISSIONS);........

但是,它显示 - 无法解析符号'CAMERA'

2 个答案:

答案 0 :(得分:10)

将以下语句导入您的代码。

import android.Manifest;

  int permissionCheckStorage = ContextCompat.checkSelfPermission(getActivity(),
                        Manifest.permission.CAMERA);
                if (permissionCheckStorage == PackageManager.PERMISSION_DENIED) {
                    requestPermissions(new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_WRITE_STORAGE);
                }

答案 1 :(得分:1)

试试这个:

if (checkSelfPermission(Manifest.permission.CAMERA)
    != PackageManager.PERMISSION_GRANTED) {

requestPermissions(new String[]{Manifest.permission.CAMERA},
        MY_REQUEST_CODE);
}

@Override
public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == MY_REQUEST__CODE) {
    if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        // Now user should be able to use camera
    }
    else {
        // Your app will not have this permission. Turn off all functions 
        // that require this permission or it will force close like your 
        // original question
    }
}
}