android / media-samples

Multiple samples showing the best practices in media APIs on Android (audio, video, etc.).
Apache License 2.0
1.27k stars 741 forks source link

MideaRecord miss permission #59

Open mr-shitou opened 4 years ago

mr-shitou commented 4 years ago

Miss Permission

diegomazala commented 4 years ago

This code editing should fix

`

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    if (!checkPermissionFromDevice())
        requestPermission();

    mPreview = findViewById(R.id.surface_view);
    captureButton = findViewById(R.id.button_capture);
}

private boolean checkPermissionFromDevice()
{
    int write_external_storage_result = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    int camera_result = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
    int record_audio_result = ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO);
    return write_external_storage_result == PackageManager.PERMISSION_GRANTED
            && camera_result == PackageManager.PERMISSION_GRANTED
            && record_audio_result == PackageManager.PERMISSION_GRANTED;
}

private void requestPermission()
{
    ActivityCompat.requestPermissions(
            this,
            new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO},
            REQUEST_PERMISSION_CODE);
}

`