
我正在尝试在我的应用程序中使用Camera2 API,即使我使用以下代码检查运行时相机权限.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) { cameraManager.openCamera(cameraID, stateCallBack, null); } else { if (shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) Toast.makeText(getApplicationContext(), "PLease allow the app to use camera app", Toast.LENGTH_LONG).show(); } ActivityCompat.requestPermissions(CaptureImageActivity.this,new String[]{"androID.manifest.permissin.CAMERA"}, CAMERA_REQUEST_RESulT); } else { cameraManager.openCamera(cameraID, stateCallBack, null); }@OverrIDepublic voID onRequestPermissionsResult(int requestCode, String[] permission, int[] grantResult) { switch (requestCode) { case CAMERA_REQUEST_RESulT: if (grantResult[0] == PackageManager.PERMISSION_GRANTED) { try { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { //this method is created because of openCamera method below i don't understand why this method is created return; } cameraManager.openCamera(cameraID, stateCallBack, null); } catch (CameraAccessException e) { e.printstacktrace(); } } if (grantResult[0] != PackageManager.PERMISSION_GRANTED) Toast.makeText(getApplicationContext(), "camera is not granted", Toast.LENGTH_LONG).show(); break; default: super.onRequestPermissionsResult(requestCode, permission, grantResult); break; }}我还有AndroIDManifest.xml文件中包含的权限.
<uses-permission androID:name="androID.permission.CAMERA" />但是,当我运行我的应用程序时,权限对话框没有显示,但是摄像头未被授予toast显示.
1)为什么权限对话框没有显示?
2)即使没有对话框显示相机如何不被授予吐司出现?我搜索了很多,但没有任何帮助!
解决方法:
这是相机API的工作运行时权限
private static final int PERMISSIONS_REQUEST_CAPTURE_IMAGE = 1; if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { // User may have declined earlIEr, ask AndroID if we should show him a reason if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) { // show an explanation to the user // Good practise: don't block thread after the user sees the explanation, try again to request the permission. } else { // request the permission. // CALLBACK_NUMBER is a integer constants ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, PERMISSIONS_REQUEST_CAPTURE_IMAGE); // The callback method gets the result of the request. } } else { }@OverrIDe public voID onRequestPermissionsResult ( int requestCode, String[] permissions, int[] grantResults){ switch (requestCode) { case PERMISSIONS_REQUEST_CAPTURE_IMAGE: { if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { // permission was granted Log.d("", "permission granted success"); } else { // permission denIEd Log.d("", "permission denIEd"); } return; } } } 总结 以上是内存溢出为你收集整理的Android相机运行时权限错误?全部内容,希望文章能够帮你解决Android相机运行时权限错误?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)