Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @AfterPermissionGranted(100)
- private void selectVideoFromGallery() {
- String[] perms = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
- if (EasyPermissions.hasPermissions(MainActivity.this, perms)) {
- Intent intent;
- if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
- intent = new Intent(Intent.ACTION_PICK, MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
- } else {
- intent = new Intent(Intent.ACTION_PICK, MediaStore.Video.Media.INTERNAL_CONTENT_URI);
- }
- intent.setType("video/*");
- intent.setAction(Intent.ACTION_GET_CONTENT);
- intent.putExtra("return-data", true);
- startActivityForResult(intent, SELECT_VIDEO_REQUEST);
- } else {
- EasyPermissions.requestPermissions(MainActivity.this, "The app needs permission to select a video from your phone",
- 100, perms);
- }
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- if (requestCode == SELECT_VIDEO_REQUEST && resultCode == RESULT_OK) {
- if (Build.VERSION.SDK_INT >= 19) {
- //Calling FileUtils class
- String sourcePath = FileUtils.getPath(getApplicationContext(), data.getData());
- Intent intent = new Intent();
- intent.setClass(MainActivity.this, PlayerActivity.class);
- intent.putExtra("videoUri", sourcePath);
- startActivity(intent);
- } else {
- //Not relevant to the question
- .......
- }
- }
- if (requestCode == SELECT_VIDEO_REQUEST && resultCode != RESULT_OK) {
- Toast.makeText(getApplicationContext(), "Failed to select video", Toast.LENGTH_LONG).show();
- } else {
- super.onActivityResult(requestCode, resultCode, data);
- }
- }
- public static String getPath(final Context context, final Uri uri) {
- final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
- // DocumentProvider
- if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
- System.out.println("getPath() uri: " + uri.toString());
- System.out.println("getPath() uri authority: " + uri.getAuthority());
- System.out.println("getPath() uri path: " + uri.getPath());
- // ExternalStorageProvider
- if ("com.android.externalstorage.documents".equals(uri.getAuthority())) {
- final String docId = DocumentsContract.getDocumentId(uri);
- final String[] split = docId.split(":");
- final String type = split[0];
- System.out.println("getPath() docId: " + docId + ", split: " + split.length + ", type: " + type);
- // This is for checking Main Memory
- if ("primary".equalsIgnoreCase(type)) {
- if (split.length > 1) {
- return Environment.getExternalStorageDirectory() + "/" + split[1] + "/";
- } else {
- return Environment.getExternalStorageDirectory() + "/";
- }
- // This is for checking SD Card
- } else {
- return "storage" + "/" + docId.replace(":", "/");
- }
- }
- }
- return null;
- }
Add Comment
Please, Sign In to add comment