Guest User

Untitled

a guest
Oct 17th, 2018
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. @AfterPermissionGranted(100)
  2. private void selectVideoFromGallery() {
  3.  
  4. String[] perms = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
  5. if (EasyPermissions.hasPermissions(MainActivity.this, perms)) {
  6. Intent intent;
  7. if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
  8. intent = new Intent(Intent.ACTION_PICK, MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
  9. } else {
  10. intent = new Intent(Intent.ACTION_PICK, MediaStore.Video.Media.INTERNAL_CONTENT_URI);
  11. }
  12.  
  13. intent.setType("video/*");
  14. intent.setAction(Intent.ACTION_GET_CONTENT);
  15. intent.putExtra("return-data", true);
  16. startActivityForResult(intent, SELECT_VIDEO_REQUEST);
  17. } else {
  18. EasyPermissions.requestPermissions(MainActivity.this, "The app needs permission to select a video from your phone",
  19. 100, perms);
  20. }
  21.  
  22. }
  23.  
  24. @Override
  25. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  26.  
  27. if (requestCode == SELECT_VIDEO_REQUEST && resultCode == RESULT_OK) {
  28.  
  29. if (Build.VERSION.SDK_INT >= 19) {
  30.  
  31. //Calling FileUtils class
  32. String sourcePath = FileUtils.getPath(getApplicationContext(), data.getData());
  33.  
  34. Intent intent = new Intent();
  35. intent.setClass(MainActivity.this, PlayerActivity.class);
  36. intent.putExtra("videoUri", sourcePath);
  37. startActivity(intent);
  38.  
  39. } else {
  40. //Not relevant to the question
  41. .......
  42.  
  43. }
  44. }
  45. if (requestCode == SELECT_VIDEO_REQUEST && resultCode != RESULT_OK) {
  46. Toast.makeText(getApplicationContext(), "Failed to select video", Toast.LENGTH_LONG).show();
  47. } else {
  48. super.onActivityResult(requestCode, resultCode, data);
  49. }
  50.  
  51.  
  52. }
  53.  
  54. public static String getPath(final Context context, final Uri uri) {
  55. final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
  56.  
  57. // DocumentProvider
  58. if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
  59. System.out.println("getPath() uri: " + uri.toString());
  60. System.out.println("getPath() uri authority: " + uri.getAuthority());
  61. System.out.println("getPath() uri path: " + uri.getPath());
  62.  
  63. // ExternalStorageProvider
  64. if ("com.android.externalstorage.documents".equals(uri.getAuthority())) {
  65. final String docId = DocumentsContract.getDocumentId(uri);
  66. final String[] split = docId.split(":");
  67. final String type = split[0];
  68. System.out.println("getPath() docId: " + docId + ", split: " + split.length + ", type: " + type);
  69.  
  70. // This is for checking Main Memory
  71. if ("primary".equalsIgnoreCase(type)) {
  72. if (split.length > 1) {
  73. return Environment.getExternalStorageDirectory() + "/" + split[1] + "/";
  74. } else {
  75. return Environment.getExternalStorageDirectory() + "/";
  76. }
  77. // This is for checking SD Card
  78. } else {
  79. return "storage" + "/" + docId.replace(":", "/");
  80. }
  81.  
  82. }
  83. }
  84. return null;
  85. }
Add Comment
Please, Sign In to add comment