Tsur Yohananov
Tsur Yohananov

Reputation: 543

Ask permission for write external storage

Im asking for permission to record audio like this:

    if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.RECORD_AUDIO)
            != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.RECORD_AUDIO},
                10);

how should I ask for write external storage and record audio?

Upvotes: 0

Views: 2810

Answers (1)

Kapil G
Kapil G

Reputation: 4141

To check for permission

if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                == PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission. RECORD_AUDIO)
                == PackageManager.PERMISSION_GRANTED) {
            Log.v(TAG,"Permission is granted");
            return true;
        } else {

            Log.v(TAG,"Permission is revoked");
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO}, 1);
            return false;
        }

Then implement onRequestPermissionsResult

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case 0:
            boolean isPerpermissionForAllGranted = false;
            if (grantResults.length > 0 && permissions.length==grantResults.length) {
                for (int i = 0; i < permissions.length; i++){
                    if (grantResults[i] == PackageManager.PERMISSION_GRANTED){
                        isPerpermissionForAllGranted=true;
                    }else{
                        isPerpermissionForAllGranted=false;
                    }
                }

                Log.e("value", "Permission Granted, Now you can use local drive .");
            } else {
                isPerpermissionForAllGranted=true;
                Log.e("value", "Permission Denied, You cannot use local drive .");
            }
            if(isPerpermissionForAllGranted){
                // Do your work here
            }
            break;
    }
}

Upvotes: 1

Related Questions