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!
This question is related to
android
video
sdk
android-intent
Use setDataAndType on the Intent
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(newVideoPath), "video/mp4");
startActivity(intent);
Use "video/mp4" as MIME or use "video/*" if you don't know the type.
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);
}
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.
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.
following code works just fine for me.
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(movieurl));
startActivity(intent);
Source: Stackoverflow.com