Skip to content

Instantly share code, notes, and snippets.

@martarodriguezm
Created October 16, 2015 06:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martarodriguezm/ddf196b2590cfae23979 to your computer and use it in GitHub Desktop.
Save martarodriguezm/ddf196b2590cfae23979 to your computer and use it in GitHub Desktop.
Code snippet for the new Android 6 permissions
private void checkWriteStoragePermission() {
//FROM AN ACTIVITY
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
}
//FROM A FRAGMENT
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
FragmentCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE: {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//Do whatever with the permission granted
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
break;
}
}
@ooscarr
Copy link

ooscarr commented Oct 17, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment