Android11取消选项后相机无法工作
创始人
2024-08-20 11:00:39
0

问题描述: Android 11上,当用户取消相机权限或访问相机时的权限请求选项后,相机可能无法正常工作。

解决步骤: 1.在AndroidManifest.xml文件中添加相机权限

2.在运行时请求相机权限,并在用户拒绝请求时显示一个对话框来解释为什么需要此权限。 if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { // Permission is not granted // Should we show an explanation? if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity, Manifest.permission.CAMERA)) { // Show an explanation to the user asynchronously -- don't block // this thread waiting for the user's response! After the user // sees the explanation, try again to request the permission. } else { // No explanation needed; request the permission ActivityCompat.requestPermissions( thisActivity, new String[]{Manifest.permission.CAMERA}, MY_PERMISSIONS_REQUEST_CAMERA); } } else { // Permission has already been granted // Initialize camera }

3.在 onRequestPermissionsResult() 回调方法中,检查是否授予权限,并根据结果进行适当的操作。 @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { switch (requestCode) { case MY_PERMISSIONS_REQUEST_CAMERA: { // If request is cancelled, the result arrays are empty. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // permission was granted, yay! Do the // camera-related task you need to do. } else { // permission denied, boo! Disable the // functionality that depends on this permission. } return; } }

4.确保在activity被销毁时释放相机资源。 @Override protected void onDestroy() { releaseCamera(); super.onDestroy(); } private void releaseCamera() { if (mCamera != null) { mCamera.release(); // release

相关内容

热门资讯

透视肯定!aapoker如何设... 透视肯定!aapoker如何设置胜率(透视)免费透视脚本(总是真的是有挂)1、玩家可以在aapoke...
透视好友!德州局透视,拱趴大菠... 透视好友!德州局透视,拱趴大菠萝挂(竟然有挂)1)拱趴大菠萝挂辅助挂:进一步探索拱趴大菠萝挂辅助透视...
透视科技!aapoker辅助怎... 透视科技!aapoker辅助怎么用(透视)透视脚本(竟然存在有挂)1、该软件可以轻松地帮助玩家将aa...
透视科技!pokemmo脚本辅... 透视科技!pokemmo脚本辅助器下载,德州私人局脚本(本来是真的有挂)1、全新机制【pokemmo...
透视规律!aapoker辅助软... 透视规律!aapoker辅助软件合法吗(透视)免费透视脚本(确实存在有挂)1、aapoker辅助软件...
透视系统!pokeplus脚本... 透视系统!pokeplus脚本,约局吧德州真的有透视挂吗(确实真的是有挂)1、约局吧德州真的有透视挂...
透视脚本!aapoker脚本(... 透视脚本!aapoker脚本(透视)脚本(果然真的是有挂);1、进入游戏-大厅左侧-新手福利-激活码...
透视安装!werplan辅助软... 透视安装!werplan辅助软件,智星德州可以透视吗(总是有挂)1、不需要AI权限,帮助你快速的进行...
透视真的!aapoker透视插... 透视真的!aapoker透视插件(透视)插件下载(都是是真的有挂)1、进入游戏-大厅左侧-新手福利-...
透视脚本!poker红龙辅助,... 透视脚本!poker红龙辅助,wepoker安装教程(总是真的有挂)1、下载好wepoker安装教程...