Android Implicit Intent for Viewing a Video File

1k views Asked by At

In my Android app, I have a button that when clicked, launches the external application of my choice to play a video (I gather that this is called an "implicit intent"). Here is the relevant Java code from my onCreate method.

Button button = (Button) findViewById(R.id.button);

button.setOnClickListener
(
    new Button.OnClickListener()
    {
        public void onClick(View v)
        {
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setDataAndType(Uri.parse("https://youtu.be/jxoG_Y6dvU8"), "video/*");
            startActivity(i);
         }
    }
);

I expected this to work, since I've followed tutorials and the Android developers documentation pretty closely, but when I test my app in the AVD, instead of prompting a menu of external applications where I can view my video, the app crashes.

What is causing my app to crash?

2

There are 2 answers

0
ganeshvjy On BEST ANSWER

Change your onClick method to below code. You should give the option to choose the external player.

@Override
public void onClick(View v) {

        Intent intent = new Intent(Intent.ACTION_VIEW);

        intent.setDataAndType(Uri.parse("https://youtu.be/jxoG_Y6dvU8"), "video/*");

        startActivity(Intent.createChooser(intent, "Complete action using"));


}
0
Bob Snyder On

Change your code to add this check:

        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setDataAndType(Uri.parse("https://youtu.be/jxoG_Y6dvU8"), "video/*");

        // Check there is an activity that can handle this intent
        if (i.resolveActivity(getPackageManager()) == null) {
            // TODO No activity available. Do something else.
        } else {
            startActivity(i);
        }