文章目录
- 1. 在AndroidManifest.xml文件中,声明相机权限:
- 2. 在你的Activity中(例如MainActivity)
- 测试
1. 在AndroidManifest.xml文件中,声明相机权限:
<uses-feature android:name="android.hardware.camera" /><uses-feature android:name="android.hardware.camera.autofocus" /><uses-feature android:name="android.hardware.camera.flash" />

2. 在你的Activity中(例如MainActivity)
- 在onCreate方法或其他合适的位置,添加检查并请求权限的逻辑:
private static final int REQUEST_CAMERA_PERMISSION = 200;

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

- onRequestPermissionsResult方法,
你可以在这里处理用户的选择。如果用户授予了权限,你可以继续执行需要相机权限的操作;如果用户拒绝了权限请求,你应该给出适当的提示,并可能禁用某些依赖于相机功能的应用部分。
@Overridepublic void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {if (requestCode == REQUEST_CAMERA_PERMISSION) {if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {} else {}}}

测试
