Advertisement
Guest User

Untitled

a guest
May 24th, 2016
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  2. if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
  3. if (ActivityCompat.shouldShowRequestPermissionRationale(this,
  4. Manifest.permission.READ_EXTERNAL_STORAGE)) {
  5. // Show an expanation to the user *asynchronously* -- don't block
  6. // this thread waiting for the user's response! After the user
  7. // sees the explanation, try again to request the permission.
  8. AlertDialog.Builder adb = new AlertDialog.Builder(this);
  9. adb.setMessage("This app needs to read your External Storage/Gallery to provide the images and the music you're going to use in the video.").setPositiveButton("OK", null).create();
  10. adb.show();
  11. } else {
  12.  
  13. // No explanation needed, we can request the permission.
  14.  
  15. ActivityCompat.requestPermissions(this,
  16. new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
  17. MY_PERMISSIONS_REQUEST_READ_EXT_STORAGE);
  18.  
  19. // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
  20. // app-defined int constant. The callback method gets the
  21. // result of the request.
  22. }
  23. }
  24. else
  25. {
  26. onCreateActual();
  27. }
  28. }
  29.  
  30. private int MY_PERMISSIONS_REQUEST_READ_EXT_STORAGE = 102;
  31.  
  32. @Override
  33. public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
  34. if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  35. Log.i("perms", permissions.length+"");
  36. int indexOfReadStorage = 0;
  37. for(int i = 0 ; i < permissions.length; i++)
  38. {
  39. Log.i("perms_len", permissions[i]);
  40. if(permissions[i].contains("READ_EXTERNAL_STORAGE"))
  41. {
  42. indexOfReadStorage = i;
  43. }
  44. }
  45. if (grantResults.length > 0 && grantResults[indexOfReadStorage] == PackageManager.PERMISSION_GRANTED) {
  46. // Permission Granted
  47. Log.i("read_ext_storage_perm", "granted");
  48. onCreateActual();
  49. } else {
  50. // Permission Denied
  51. Toast.makeText(this, "READ_EXT_STORAGE Denied", Toast.LENGTH_SHORT)
  52. .show();
  53. }
  54. }
  55. super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement