Skip to content

Instantly share code, notes, and snippets.

@vxhviet
Last active April 23, 2023 11:22
Show Gist options
  • Save vxhviet/be353e28288e79f8d9e7 to your computer and use it in GitHub Desktop.
Save vxhviet/be353e28288e79f8d9e7 to your computer and use it in GitHub Desktop.
How to select a video from the gallery and get it's real path?

Source: StackOverflow & StackOverflow

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

Answer: Here is the full code for 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 selectedVideoUri = data.getData();

            // OI FILE Manager
            //filemanagerstring = selectedImageUri.getPath();

            // MEDIA GALLERY
            String selectedVideoPath = getPath(selectedVideoUri);
            if (selectedVideoPath != null) {

                Intent intent = new Intent(HomeActivity.this,
                        VideoplayAvtivity.class);
                intent.putExtra("path", selectedVideoPath);
                startActivity(intent);
            }
        }
    }
}

public String getPath(Uri uri) {
    Cursor cursor = null;
    try {
        String[] projection = {MediaStore.Images.Media.DATA};
        cursor = getContentResolver().query(uri, projection, null, null, null);

        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}
@0000mprakash
Copy link

0000mprakash commented Apr 23, 2023

what changes i would have to do if i want the path of an xls file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment