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