64

I'm trying to play video's on Android, by launching an intent. The code I'm using is:

tostart = new Intent(Intent.ACTION_VIEW);
tostart.setDataAndType(Uri.parse(movieurl), "video/*");
startActivity(tostart); 

This works on most phones, but not on the HTC Hero. It seems to load a bit different video player. This does play the first video thrown at it. However, every video after that it doesn't respond. (it keeps in some loop).

If I add an explicit

tostart.setClassName("com.htc.album","com.htc.album.ViewVideo");

(before the startactivity) it does work on the HTC Hero. However, since this is a HTC specific call, I can't run this code on other phones (such as the G1). On the G1, this works:

tostart.setClassName("com.android.camera","com.android.camera.MovieView"); //g1 version

But this intent is missing from the hero. Does anybody know a list of intents/classnames that should be supported by all Android devices? Or a specific one to launch a video? Thanks!

N J
  • 25,967
  • 13
  • 73
  • 94
PanMan
  • 1,210
  • 1
  • 9
  • 15

6 Answers6

91

Use setDataAndType on the Intent

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(newVideoPath));
intent.setDataAndType(Uri.parse(newVideoPath), "video/mp4");
startActivity(intent);

Use "video/mp4" as MIME or use "video/*" if you don't know the type.

Edit: This not valid for general use. It fixes a bug in old HTC devices which required the URI in both the intent constructor and be set afterwards.

Christian
  • 1,104
  • 8
  • 9
  • 8
    Save the parsed uri to a variable. There's no need parse it twice. – marden Feb 13 '13 at 13:17
  • 2
    -1 That is exactly what the original poster had used (setDataAndType) – Russ Feb 28 '13 at 00:19
  • 1
    I don't get why this answer got so many upvotes.. It solves not a single dime of the posted question... – Carsten Hoffmann Nov 13 '14 at 00:55
  • 6
    I know it shouldn't - but it does... Adding the uri BOTH in the intent constructor and setDataAndType makes some HTC devices play the video. That's the small difference from what PanMan originally tried. – Christian Nov 14 '14 at 08:10
  • No Activity found to handle Intent { act=android.intent.action.VIEW dat=/storage/emulated/0/DCIM/Camera/VID_۲۰۱۶۰۳۰۶_۰۰۳۲۳۸.mp4 typ=video/mp4 } :( – abbasalim Mar 06 '16 at 06:14
  • 3
    update: this worked: intent.setDataAndType(Uri.fromFile(new File(path)), "video/mp4"); – abbasalim Mar 06 '16 at 06:24
  • 4
    This won't work in API 24+, due to file:// scheme is now not allowed to be attached with Intent on targetSdkVersion 24. – Binoy Babu Sep 08 '16 at 15:04
  • working on API 28. Tested on Nexus 5 with web url video – Rahul Khurana Jan 24 '19 at 05:38
  • I'm making a Video player app and from another app i'm opening video using this code,now can anyone tell me how to get video details in Video Player app from this intent – Vivek Thummar Jan 28 '20 at 07:38
  • why do you set second paramter? it's the same as calling `setData()`, but then you call `setDataAndType()` anyway – user924 Jun 10 '20 at 15:12
  • @marden no need to to set it twice as well – user924 Jun 10 '20 at 15:24
18

From now onwards after API 24, Uri.parse(filePath) won't work. You need to use this

final File videoFile = new File("path to your video file");
Uri fileUri = FileProvider.getUriForFile(mContext, "{yourpackagename}.fileprovider", videoFile);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(fileUri, "video/*");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//DO NOT FORGET THIS EVER
startActivity(intent);

But before using this you need to understand how file provider works. Go to official document link to understand file provider better.

Kishan Solanki
  • 8,893
  • 2
  • 51
  • 54
  • `java.lang.IllegalArgumentException: Couldn't find meta-data for provider with authority com.example.videotest.fileprovider` – user924 Jun 10 '20 at 15:30
11

I have come across this with the Hero, using what I thought was a published API. In the end, I used a test to see if the intent could be received:

private boolean isCallable(Intent intent) {
    List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent, 
        PackageManager.MATCH_DEFAULT_ONLY);
    return list.size() > 0;
}

In use when I would usually just start the activity:

final Intent intent = new Intent("com.android.camera.action.CROP");
intent.setClassName("com.android.camera", "com.android.camera.CropImage");
if (isCallable(intent)) {
    // call the intent as you intended.
} else {
    // make alternative arrangements.
}

obvious: If you go down this route - using non-public APIs - you must absolutely provide a fallback which you know definitely works. It doesn't have to be perfect, it can be a Toast saying that this is unsupported for this handset/device, but you should avoid an uncaught exception. end obvious.


I find the Open Intents Registry of Intents Protocols quite useful, but I haven't found the equivalent of a TCK type list of intents which absolutely must be supported, and examples of what apps do different handsets.

jamesh
  • 18,935
  • 13
  • 54
  • 96
11

following code works just fine for me.

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(movieurl));
startActivity(intent);
Chang
  • 119
  • 1
  • 2
  • So thats fine, but how to stop the audio once the user presses the back button on the Audio Screen. The audio just keeps on playing. – AndroidDev Dec 30 '11 at 10:59
  • It works but it opens the browser first because it doesn't know that it is a video. I *do* know that it is a video so I want to pass the URL directly to a video player. – Timmmm Jul 10 '12 at 12:33
0

from the debug info, it seems that the VideoIntent from the MainActivity cannot send the path of the video to VideoActivity. It gives a NullPointerException error from the uriString. I think some of that code from VideoActivity:

Intent myIntent = getIntent();
String uri = myIntent.getStringExtra("uri");
Bundle b = myIntent.getExtras();

startVideo(b.getString(uri));

Cannot receive the uri from here:

public void playsquirrelmp4(View v) {
    Intent VideoIntent = (new Intent(this, VideoActivity.class));
    VideoIntent.putExtra("android.resource://" + getPackageName()
        + "/"+   R.raw.squirrel, uri);
    startActivity(VideoIntent);
}
N J
  • 25,967
  • 13
  • 73
  • 94
Leinad
  • 19
  • 1
0

First you need to convert path to real path. For example if you have path like content://folder/123 You need to convert it to path like foldername/fil.mp4 with Environment.getExternalStorageDirectory()

So your path string will be: String path = Environment.getExternalStorageDirectory() + "foldername/file.mp4"; Then you need to convert it to file:

File file = new File(path);

And in the end use this in the line:

intent.setDataAndType(Uri.fromFile(file), "video/*");

SWIK
  • 179
  • 2
  • 7