Skip to content

Instantly share code, notes, and snippets.

@anstaendig
Last active September 3, 2017 00:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anstaendig/55636adaa23cbfcc14711afeeac4b390 to your computer and use it in GitHub Desktop.
Save anstaendig/55636adaa23cbfcc14711afeeac4b390 to your computer and use it in GitHub Desktop.
permissions
// Ask for permission to read storage
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.READ_EXTERNAL_STORAGE)) {
// This will happen if the user previously denied the permission
requestPermissions()
} else {
// We need to check if it's the first time we ask because
// shouldShowRequestPermissionRationale returns false in that case.
// We set a boolean (shared prefs for example) to store if we are asking for the first time or not
if (isFirstTimeAskingPermission(Manifest.permission.READ_EXTERNAL_STORAGE)) {
firstTimeAskingPermission(Manifest.permission.READ_EXTERNAL_STORAGE, false)
// No explanation needed, we can request the permission.
requestPermissions()
} else {
// Permission disable by device policy or user denied permanently. Show dialog to
// redirect to application settings
showDialogToNavigateToApplicationSettings()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment