Android 6.0 Dynamic Request audio + Photo + album permissions

Source: Internet
Author: User

1. Audio permissions (including recording and playback)

1.1. Add two permissions to the list first

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

1.2. Then execute the following code in the OnCreate of the activity or any other place where it must be executed

if(packagemanager.permission_granted = =Contextcompat. Checkselfpermission (Context, Android. Manifest.permission.RECORD_AUDIO)) {}Else{        //Prompt user to open an account right audioString[] perms = {"Android.permission.RECORD_AUDIO"}; Activitycompat.requestpermissions (chatactivity. This, perms, Result_code_startaudio); }
@Override
Public voidOnrequestpermissionsresult (intPermsrequestcode, string[] permissions,int[] grantresults) { Switch(permsrequestcode) { CaseResult_code_startaudio:Booleanalbumaccepted = grantresults[0]==packagemanager.permission_granted; if(!albumaccepted) {toastutil.show (context,"Please turn on app recording permission"); } Break; } }


2. Permission to take photos

2.1. First add two permissions to the list

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

2.2. Then add the following code to the oncreate of the activity or to any other place where it must be executed

    //determine whether to open a photo album permissions    if(packagemanager.permission_granted = =contextcompat.checkselfpermission (Context, Android.    Manifest.permission.CAMERA)) {Camera.startcameraurl (context, filename, CAMERA); }Else{        //prompt the user to open a permissionString[] perms = {"Android.permission.CAMERA"}; Activitycompat.requestpermissions (broadcast_publish. This, perms, Result_code_startcamera); } @Override Public voidOnrequestpermissionsresult (intPermsrequestcode, string[] permissions,int[] grantresults) {        Switch(permsrequestcode) { CaseResult_code_startcamera:Booleancameraaccepted = grantresults[0]==packagemanager.permission_granted; if(cameraaccepted) {//after the authorization is successful, call the system camera to take a photo operation, etc.camera.startcameraurl (context, filename, Camera); }Else{                    //after the user authorizes the refusal, the friendly hint will be ableToastutil.show (Context, "Please open app photo permission"); }                 Break; }    }


3. Open Album Permissions

3.1. You need to include the following permissions in the list

<!--write the SD card permissions--><uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/><!-- Read SD card permissions--><uses-permission android:name= "Android.permission.READ_EXTERNAL_STORAGE"/><uses-permission Android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

3.2. Then execute the following code in the OnCreate of the activity or the method that is bound to execute

    //The second parameter is the permission to request    if(Contextcompat.checkselfpermission ( This, Manifest.permission.WRITE_EXTERNAL_STORAGE)! =packagemanager.permission_granted) {        //permissions have not been granted, you need to write code to request permission hereActivitycompat.requestpermissions ( This,                Newstring[]{manifest.permission.write_external_storage}, Permissions_request_open_album); }Else {        //permissions have been granted, here directly write the appropriate method to execute} @Override Public voidOnrequestpermissionsresult (intRequestcode, string[] permissions,int[] grantresults) {        if(Requestcode = =my_permissions_request_open_album) {            if(Grantresults[0] = =packagemanager.permission_granted) {                //Authorized Success            } Else {                //Authorization FailureToast.maketext ( This, "Permission Denied", Toast.length_short). Show (); }        }    }


Android 6.0 Dynamic Request audio + Photo + album permissions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.