关于检测手机摄像头权限的问题

来源:互联网 发布:新还珠格格演员表知画 编辑:程序博客网 时间:2024/04/29 15:44

摄像头权限时系统的危险权限,对于6.0以上和6.0以下系统有着不同的检测方法。

6.0以上系统:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//6.0以上系统            Log.e(TAG, "onCreate: 6.0以上系统" );            try {                //获得程序targetSdkVersion                PackageInfo info = getPackageManager().getPackageInfo(getPackageName(),0);                int targetSdkVersion = info.applicationInfo.targetSdkVersion;                if(targetSdkVersion>23){//targetSdkVersion>23时使用ContextCompat.checkSelfPermission()检查权限                    if(ContextCompat.checkSelfPermission(this, needPermissions[0]) != PackageManager.PERMISSION_GRANTED                            || ActivityCompat.shouldShowRequestPermissionRationale(this, needPermissions[0])){                        //未授权                        showMissingPermissionDialog();                    }else{//已授权                        initView();                    }                }else{//targetSdkVersion>23时使用PermissionChecker.checkSelfPermission检查权限                    if(PermissionChecker.checkSelfPermission(this,needPermissions[0]) == PermissionChecker.PERMISSION_GRANTED){                        //已授权                        initView();                    }else{                        //未授权                        showMissingPermissionDialog();                    }                }            } catch (PackageManager.NameNotFoundException e) {                e.printStackTrace();            }        }

6.0以下系统:系统没有直接提供检测的API,只能判断系统摄像头是否可用

public static boolean cameraIsCanUse() {        boolean isCanUse = true;        Camera mCamera = null;        try {            mCamera = Camera.open();            Camera.Parameters mParameters = mCamera.getParameters();            mCamera.setParameters(mParameters);        } catch (Exception e) {            isCanUse = false;        }        if (mCamera != null) {            try {                mCamera.release();            } catch (Exception e) {                e.printStackTrace();                return isCanUse;            }        }        return isCanUse;    }

这样大概可以设配大多数手机,本人测试时候只用了两台机器,一个是6.0以上的小米note手机,一个是6.0以下的华为SCL-TL00H型号手机,如若有什么问题,欢迎大家指教

阅读全文
0 0