Judge whether Android mobile phone (OPPO mobile phone) has recording permission compatible with less than 6.0

Keywords: Mobile Android

In the project, when the old version of OPPO mobile 5.1 is encountered, the recording permission is explicitly denied, but the callback is still true. Since 6.0, the runtime permission has been issued, and the way to obtain the permission has become unified. First, configure the required permission in the list file, and then take photos or record to call when using

//Dynamic access above 6.0
    if (ContextCompat.checkSelfPermission(MainActivity.this, 
Manifest.permission.CAMERA) != 
        //Request, take, photo, permission is a custom constant
                new String[]{Manifest.permission.CAMERA},

This is to call up the pop-up window to apply for permission

public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if (requestCode == REQUEST_TAKE_PHOTO_PERMISSION) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            //The application is successful, you can take photos
        } else {
            Toast.makeText(MainActivity.this,"You have denied permission, this function is not available\n You can authorize photo taking in the application settings",Toast.LENGTH_SHORT).show();
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

so, in order to be compatible with the old version, we can't just use the official way to determine the permission application, but use the MinBufferSize stream to determine whether it can record

 * Function:Judge recording authority, compatible with Android 6.0 and below and above systems 
public class CheckAudioPermission {  
    // Audio acquisition source  
    public static int audioSource = MediaRecorder.AudioSource.MIC;  
    // Set the audio sampling rate, 44100 is the current standard, but some devices still support 2205016000111025  
    public static int sampleRateInHz = 44100;  
    // Set the recorded channel of audio channel in stereo as dual channel and channel configuration mono as mono channel  
    public static int channelConfig = AudioFormat.CHANNEL_IN_STEREO;  
    // Audio data format: PCM 16 bits per sample. Ensure equipment support. PCM 8 bits per sample. Not necessarily supported by the device.  
    public static int audioFormat = AudioFormat.ENCODING_PCM_16BIT;  
    // Buffer byte size  
    public static int bufferSizeInBytes = 0;  
     * Determine whether there is recording permission 
    public static boolean isHasPermission(final Context context){  
        bufferSizeInBytes = 0;  
        bufferSizeInBytes = AudioRecord.getMinBufferSize(sampleRateInHz,  
                channelConfig, audioFormat);  
        AudioRecord audioRecord =  new AudioRecord(audioSource, sampleRateInHz,  
                channelConfig, audioFormat, bufferSizeInBytes);  
        //Start recording audio  
            // Prevent some mobile phones from crashing, such as Lenovo  
        }catch (IllegalStateException e){  
         * Judge whether there is recording authority according to the start of recording 
        if (audioRecord.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING) {  
            return false;  
        audioRecord = null;  
        return true;  

Posted by unknownsheep on Thu, 30 Apr 2020 08:16:49 -0700