code: private void requestPermissions(int mediaType) {
          if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                  != PackageManager.PERMISSION_GRANTED) {
              if (mediaType == TYPE_IMAGE) {
                  ActivityCompat.requestPermissions(this,
                          new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                          MY_PERMISSIONS_REQUEST_WRITE_STORAGE);
              } else {
                  ActivityCompat.requestPermissions(this,
                          new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                          MY_PERMISSIONS_REQUEST_WRITE_STORAGE_VID);
              }
  
          } else {
              if (mediaType == TYPE_IMAGE) {
                  // Want to compress an image
                  dispatchTakePictureIntent();
              } else if (mediaType == TYPE_VIDEO) {
                  // Want to compress a video
                  dispatchTakeVideoIntent();
              }
  
          }
      }
docs: Request Permission for writing to External Storage in 6.0 and up