I'm experiencing a weird error with video Intents
that I haven't experienced until Android 4.1.
Here's my code for launching the Intent
. I've tried with other MIME types as well, including video/mp4, but the wildcard (*video/**) is supposed to work just fine, according to the official Android developer site on Intents and Intent filters.
Intent videoIntent = new Intent();
videoIntent.setAction(Intent.ACTION_VIEW);
videoIntent.setData(Uri.parse(fileUrl));
videoIntent.setType("video/*");
startActivity(videoIntent);
On my devices, both running Android 4.1, this results in an ActivityNotFoundException
, because it says no installed applications can handle the Intent
. This is weird because it's been working on all previous versions of Android, and it should launch in the default video player.
Many third party video players are capable of handling the Intent
, so I'm wondering why it's not working with the default video player anymore.
Any ideas?
This seems to be the same issue:
Video player not working on Jelly Bean device :android.content.ActivityNotFoundException
I managed to find a solution with a bit of help from someone else. It seems that the setType()
call clears any previously attached data, including the setData()
call. Per the documentation:
This method automatically clears any data that was previously set (for example by setData(Uri)).
When I changed it to setDataAndType()
instead, it worked. It appears that it wasn't an issue with Jelly Bean after all, thankfully :-)
Here's the final code for creating the video intent:
public static Intent getVideoIntent(String fileUrl) {
Intent videoIntent = new Intent(Intent.ACTION_VIEW);
videoIntent.setDataAndType(Uri.fromFile(new File(fileUrl)), getMimeType(fileUrl));
return videoIntent;
}
public static String getMimeType(String url) {
String type = null;
String extension = MimeTypeMap.getFileExtensionFromUrl(url);
if (extension != null) {
MimeTypeMap mime = MimeTypeMap.getSingleton();
type = mime.getMimeTypeFromExtension(extension);
if (TextUtils.isEmpty(type))
type = "video/*"; // No MIME type found, so use the video wildcard
}
return type;
}