2

I have been trying to get my local videos (raw folder) to run using Intent.ActionView. When I create the Intent and set a data and type then start the activity I get a error. Please look at the following code & error.

I have tried a load of different ways to get to the file thinking maybe it is not being found but I don't think that is the problem. I have tried different video formats but also I do not think that is the problem.

If I use a URL to a MP4 inside SetDataAndType that will run without an error. Maybe this is a permissions error? At this point I really have no idea. I hope someone will be able to explain the problem I an running into here.

Code:

// Create the intent and push the mp4 to it, lets run a video!
Intent videoIntent = new Intent (Intent.ActionView);

// Video is MP4 format
videoIntent.SetDataAndType (Uri.Parse ("android.resource://" + PackageName + "/raw/bunny"), "video/*");

// Crash here, Error: Android.Content.ActivityNotFoundExeception
StartActivity (videoIntent);

Error:

    Android.Content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=android.resource://com.test.videotest/raw/bunny typ=video/* }
      at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/3053/a94a03b5/source/mono/external/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143
      at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (IntPtr jobject, IntPtr jclass, IntPtr jmethod, Android.Runtime.JValue* parms) [0x00084] in /Users/builder/data/lanes/3053/a94a03b5/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:1029
      at Android.Content.ContextWrapper.StartActivity (Android.Content.Intent intent) [0x00070] in /Users/builder/data/lanes/3053/a94a03b5/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.Content.ContextWrapper.cs:3238
      at VideoTest.MainActivity.<OnCreate>m__0 (System.Object , System.EventArgs ) [0x00034] in /Users/alex/Projects/VideoTest/VideoTest/MainActivity.cs:33
      at Android.Views.View+IOnClickListenerImplementor.OnClick (Android.Views.View v) [0x0000d] in /Users/builder/data/lanes/3053/a94a03b5/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.Views.View.cs:1780
      at Android.Views.View+IOnClickListenerInvoker.n_OnClick_Landroid_view_View_ (IntPtr jnienv, IntPtr native__this, IntPtr native_v) [0x00011] in /Users/builder/data/lanes/3053/a94a03b5/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.Views.View.cs:1745
      at at (wrapper dynamic-method) System.Object:123eb72a-af75-42a8-b3aa-717e415176e4 (intptr,intptr,intptr)
      at --- End of managed exception stack trace ---
      at android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=android.resource://com.test.videotest/raw/bunny typ=video/* }
      at at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1798)
      at at android.app.Instrumentation.execStartActivity(Instrumentation.java:1512)
      at at android.app.Activity.startActivityForResult(Activity.java:3930)
      at at android.app.Activity.startActivityForResult(Activity.java:3890)
      at at android.app.Activity.startActivity(Activity.java:4213)
      at at android.app.Activity.startActivity(Activity.java:4181)
      at at mono.android.view.View_OnClickListenerImplementor.n_onClick(Native Method)
      at at mono.android.view.View_OnClickListenerImplementor.onClick(View_OnClickListenerImplementor.java:29)
      at at android.view.View.performClick(View.java:5204)
      at at android.view.View$PerformClick.run(View.java:21155)
      at at android.os.Handler.handleCallback(Handler.java:739)
      at at android.os.Handler.dispatchMessage(Handler.java:95)
      at at android.os.Looper.loop(Looper.java:148)
      at at android.app.ActivityThread.main(ActivityThread.java:5422)
      at at java.lang.reflect.Method.invoke(Native Method)
      at at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
      at at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Note:

You can easily try this yourself using the following:

  1. Create a new Xamarin Android App
  2. Copy the above code into the button click (pre-made with the project)
  3. Create a raw folder in "Resources" and put any MP4 file inside (name it bunny or rename the video in the SetDataAndType)
  4. Run the application on a device (simulator does not seem to handle this so I used my Nexus 5X)

Further more:

My device has both VLC Player and Google Photos installed, both of these can play MP4 files. And as proof if I change the data to a URL (linking to a MP4 file) this will work fine.

This is not a matter of the file not being found as I have tested the same path using a VideoView and it works fine.

Ruddy
  • 9,795
  • 5
  • 45
  • 66
  • Possible duplicate of [How to play videos in android from assets folder or raw folder?](http://stackoverflow.com/questions/3028717/how-to-play-videos-in-android-from-assets-folder-or-raw-folder) – Jason Apr 12 '16 at 15:02
  • @Jason Not relevant I'm afraid. I fear this is more a Xamarin problem more then anything. The file is being found if I use other methods sure as VideoView. – Ruddy Apr 12 '16 at 15:04

2 Answers2

1

This is happening because there is no app in your device or emulator that can handle mp4 files.

To avoid crash you can do something like this

PackageManager packageManager = getPackageManager();
if (intent.resolveActivity(packageManager) != null) {
    // this intent can be handled
    startActivity(intent);
}   else  {
    // no app found that can handle the intent
    Toast.makeText(context, "No Intent available to handle action", Toast.LENGTH_LONG).show;
}
drulabs
  • 3,071
  • 28
  • 35
  • I should have posted this on the question. My device has VLC and Google Photos installed.Both can handle playing MP4. As I said in my question if I change the data to a URL (a MP4 file) it will work fine. – Ruddy Apr 12 '16 at 15:00
1

try this think its something todo with it not finding the uri correctly:

  videoIntent.SetDataAndType (Uri.FromFile (new File($"android.resource://{PackageName}/{Resource.Raw.bunny}")), "video/*");

this then open gives me the choice of Photos or Video player.

But I think this just hides the fact it can load the video as SushiHangover said.

Is there any reason you can use a ViewVideo in another Activity like so:

 [Activity (Label = "VideoActivity")]           
 public class VideoActivity : Activity
 {
        // Create your application here
        VideoView videoView;
        protected override void OnCreate (Bundle bundle)
        {
        base.OnCreate (bundle);

        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.VideoActivity);
        videoView = FindViewById<VideoView> (Resource.Id.SampleVideoView);
        videoView.SetMediaController(new MediaController(this));
        videoView.SetVideoPath ($"android.resource://{PackageName}/{Resource.Raw.bunny}");
        videoView.RequestFocus ();
        videoView.Start ();
    }
}

And layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


 <VideoView 
    android:id="@+id/SampleVideoView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>  

</LinearLayout>

EDIT

The '$' at the start is a c#6 String interpolation https://msdn.microsoft.com/en-GB/library/dn961160.aspx

In regards to solution, I think that putting a video file in the Raw folder in the app will mean that any intents wont be able to play it as its in the app's directory. So the best options are to either some how get the video into a playable folder (copying it to sd card NOT Ideal either) or play the video using a videoView

Iain Smith
  • 9,230
  • 4
  • 50
  • 61
  • The problem with `Uri.Parse` is your are passing a "resource://" scheme to an intent that can not handle it, in using `FromFile` you end up with a `file://` scheme that is a video reference that Android OS can find an intent for (or multiple ones in this case) – SushiHangover Apr 12 '16 at 15:47
  • TBH this is not a great solution think there will be permission issues. Is there any reason you cant play the file from a videoView? – Iain Smith Apr 12 '16 at 15:56
  • Your first line throws an error because of `$` in `new File($`. Not sure what the `$` is, if you could explain that would be great. Also the reason I cant use `VideoView` is that it is very glitchy with fullscreen (immersive mode). On devices with no physical buttons you can "hardware buttons" onscreen. When using the `VideoView` these buttons overlap the video controls (on some devices) and on others when you click the video it resets the ui flags so it comes out of fullscreen immersive mode. – Ruddy Apr 13 '16 at 07:25
  • @IainSmith Maybe we could start a chat and talk about this a bit more? I have tried a lot of things to get this working. I will be putting a bounty on the question at some point too. – Ruddy Apr 13 '16 at 07:29