Search code examples
androidandroid-fileprovider

How to play videos with phone's standard video player app


I want to play videos i have stored in the external storage with the phone's standard video Player app. I've tried using FileProvider, but I can't manage to pass the video to the player.

private void passVideo(String videoname){
        File videoPath = new File(Environment.getExternalStorageDirectory(), "video_folder");
        File newFile = new File(videoPath, videoname);
        Uri path = FileProvider.getUriForFile(this, "com.example.provider", newFile);
        Intent shareIntent = ShareCompat.IntentBuilder.from(this)
                .setType(getContentResolver().getType(path))
                .setStream(path)
                .getIntent();
        shareIntent.setData(path);
        shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        startActivity(Intent.createChooser(shareIntent, "Open Video..."));
    }

With this code I manage to get the Chooser for gmail, whatsapp and other social media platforms, but that's not what I want and they all say they can't handle the file format anyway. It also gives the option to play the video with VLC, but it instantly crashes. I have tried every possible file format and none of them works.

Sorry if I'm missing something obvious, I am still a beginner.


Solution

  • ShareCompat.IntentBuilder is for ACTION_SEND, which is not the typical Intent action for playing a video. ACTION_VIEW would be more typical. So, try:

    private void passVideo(String videoname){
        File videoPath = new File(Environment.getExternalStorageDirectory(), "video_folder");
        File newFile = new File(videoPath, videoname);
        Uri uri = FileProvider.getUriForFile(this, "com.example.provider", newFile);
        Intent viewIntent = new Intent(Intent.ACTION_VIEW, uri);
    
        viewIntent.setType(getContentResolver().getType(uri));
        viewIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        startActivity(viewIntent);
    }