Created
October 16, 2015 06:19
-
-
Save martarodriguezm/ddf196b2590cfae23979 to your computer and use it in GitHub Desktop.
Code snippet for the new Android 6 permissions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Found here http://www.tipsfordevelopers.com/android-marshmallow-permissions-part-1/