Android intent for playing video? Android intent for playing video? android android

Android intent for playing video?


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.


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 EVERstartActivity(intent);

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


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.