检查有无相机的权限
public void actionCheckPermission(View view) { String perms = Manifest.permission.CAMERA; Integer nRet = 0; nRet = ContextCompat.checkSelfPermission(this, perms); if (nRet == PERMISSION_GRANTED) { Toast.makeText(this, "有权限", Toast.LENGTH_LONG).show(); } else if(nRet == PERMISSION_DENIED) { Toast.makeText(this, "没有权限", Toast.LENGTH_LONG).show(); } else { Toast.makeText(this, "非法返回值", Toast.LENGTH_LONG).show(); } }
手机权限设置:
设置->授权管理->应用权限管理->权限管理
动态申请相机权限:
public void actionRequestPermission(View view) { if(Build.VERSION.SDK_INT >= 23) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA},123); } else {} } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if(requestCode == 123) { if(grantResults[0] == PERMISSION_GRANTED) { Toast.makeText(this, "申请权限成功", Toast.LENGTH_LONG).show(); } else if(grantResults[0] == PERMISSION_DENIED) { Toast.makeText(this, "申请权限失败", Toast.LENGTH_LONG).show(); } else { } } }