Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select a video from the gallery and get it's real path?

Tags:

I can open the gallery with this code,

    Intent intent = new Intent(Intent.ACTION_PICK);     intent.setType("video/*");      intent.setAction(Intent.ACTION_GET_CONTENT);     startActivityForResult(Intent.createChooser(intent, "Select Video"), PICK_VIDEO_REQUEST ); 

It works well on a Samsung Galaxy S5. But in some phones, images are displayed along with videos.

What changes do I need to do to the code to open the gallery to display only videos?

How can I get the real path of the selected video when the results are returned?

like image 661
Prashant Avatar asked Jun 25 '15 08:06

Prashant


Video Answer


2 Answers


Here is the full code to get the video path after selecting from gallery.

Intent intent = new Intent(); intent.setType("video/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser(intent,"Select Video"),REQUEST_TAKE_GALLERY_VIDEO);  public void onActivityResult(int requestCode, int resultCode, Intent data) {         if (resultCode == RESULT_OK) {             if (requestCode == REQUEST_TAKE_GALLERY_VIDEO) {                 Uri selectedImageUri = data.getData();                  // OI FILE Manager                 filemanagerstring = selectedImageUri.getPath();                  // MEDIA GALLERY                 selectedImagePath = getPath(selectedImageUri);                 if (selectedImagePath != null) {                      Intent intent = new Intent(HomeActivity.this,                             VideoplayAvtivity.class);                     intent.putExtra("path", selectedImagePath);                     startActivity(intent);                 }             }         }     }      // UPDATED!     public String getPath(Uri uri) {         String[] projection = { MediaStore.Video.Media.DATA };         Cursor cursor = getContentResolver().query(uri, projection, null, null, null);         if (cursor != null) {             // HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL             // THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA             int column_index = cursor                     .getColumnIndexOrThrow(MediaStore.Video.Media.DATA);             cursor.moveToFirst();             return cursor.getString(column_index);         } else             return null;     } 
like image 62
Rohit Heera Avatar answered Sep 22 '22 15:09

Rohit Heera


Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Video.Media.EXTERNAL_CONTENT_URI);  startActivityForResult(intent , SELECT_VIDEO); 
like image 27
EminenT Avatar answered Sep 19 '22 15:09

EminenT