Android 6.0 动态申请 音频+拍照+相册 权限

1.音频的权限(包括录音和播放)

1.1.首先要在清单中加上两个权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />

 

1.2.然后在活动的onCreate或者其他必然执行的地方执行如下代码 

    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){ ToastUtil.show(context,"请开启应用录音权限"); } break; } }


2.拍照的权限

2.1.首先在清单中加入两个权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>

 

2.2.然后在活动的onCreate或者其他必然执行的地方加入如下代码 

    //判断是否开户相册权限
    if (PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(context, android.Manifest.permission.CAMERA)) {
        Camera.startCameraUrl(context, filename, 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){
                    //授权成功之后,调用系统相机进行拍照操作等
                    Camera.startCameraUrl(context, filename, CAMERA);
                }else{
                    //用户授权拒绝之后,友情提示一下就可以了
                    ToastUtil.show(context,"请开启应用拍照权限");
                }
                break;
        }
    }


3.打开相册权限

3.1.需要在清单中加入如下权限

<!-- 写sd卡的权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!-- 读sd卡权限 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />

 

3.2.然后在活动的onCreate或者必然执行的方法执行如下代码

    //第二个参数是需要申请的权限
    if (ContextCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
        //权限还没有授予,需要在这里写申请权限的代码
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSIONS_REQUEST_OPEN_ALBUM);
    }else {
        //权限已经被授予,在这里直接写要执行的相应方法即可

    }
    
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == MY_PERMISSIONS_REQUEST_OPEN_ALBUM) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                //授权成功

            } else {
                //授权失败
                Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show();
            }
        }
    }


posted @ 2017-12-06 11:20  Jason_Jan  阅读(8003)  评论(0编辑  收藏  举报