美文网首页
Android6.0+ 音频、拍照权限

Android6.0+ 音频、拍照权限

作者: 琦琦出去玩了 | 来源:发表于2018-01-30 11:19 被阅读65次

    开启音频权限

    if (PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(context, android.Manifest.permission.RECORD_AUDIO)) {
         //正常逻辑
    } else {
        //提示用户开户权限音频
        String[] perms = {"android.permission.RECORD_AUDIO"};
        ActivityCompat.requestPermissions(ChatActivity.this,perms, RESULT_CODE_STARTAUDIO);
    }
    @Override
    public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults){
     switch(permsRequestCode){
         case RESULT_CODE_STARTAUDIO:
                boolean albumAccepted = grantResults[0]==PackageManager.PERMISSION_GRANTED;
                if(!albumAccepted){
                       Toast.makeText(this, "请开启音频权限", Toast.LENGTH_SHORT).show();
                }
                break;
         }
    }
    

    开启拍照权限

    //判断是否开户相册权限
    if (PackageManager.PERMISSION_GRANTED ==   ContextCompat.checkSelfPermission(context, android.Manifest.permission.CAMERA)) {
       //授权成功之后,调用系统相机进行拍照操作等
    } else {
       //提示用户开户权限
       String[] perms = {"android.permission.CAMERA"};     
       ActivityCompat.requestPermissions(Broadcast_publish.this,perms, RESULT_CODE_STARTCAMERA);
    }
    @Override
    public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults){
       switch(permsRequestCode){
             case RESULT_CODE_STARTCAMERA:
                    boolean cameraAccepted = grantResults[0]==PackageManager.PERMISSION_GRANTED;
                    if(cameraAccepted){
                        //授权成功之后,调用系统相机进行拍照操作等
                    }else{
                        //用户授权拒绝之后,友情提示一下就可以了
                        Toast.makeText(this, "请开启拍照权限", Toast.LENGTH_SHORT).show();
                    }
                    break;
       }
    }
    
    

    相关文章

      网友评论

          本文标题:Android6.0+ 音频、拍照权限

          本文链接:https://www.haomeiwen.com/subject/rqgqzxtx.html