为什么Intent.ActionView会抛出MP4文件的错误

时间:2016-04-12 14:44:12

标签: c# android android-intent xamarin xamarin.android

我一直试图让我的本地视频(原始文件夹)使用Intent.ActionView运行。当我创建Intent并设置数据和类型然后启动活动时我收到错误。请查看以下代码&错误。

我已经尝试了一些不同的方法来获取文件,但可能没有找到,但我不认为这是问题所在。我尝试过不同的视频格式,但我认为这不是问题所在。

如果我在一个没有错误的情况下运行SetDataAndType内的MP4的URL。也许这是一个权限错误?此时我真的不知道。我希望有人能够解释我遇到的问题。

代码:

// 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);

错误:

    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)

注意:

您可以使用以下方法轻松尝试:

  1. 创建新的Xamarin Android应用
  2. 将上述代码复制到按钮单击(使用项目预先制作)
  3. 在&#34;资源&#34;中创建一个原始文件夹。并将任何MP4文件放入其中(将其命名为bunny或将视频重命名为SetDataAndType
  4. 在设备上运行应用程序(模拟器似乎没有处理这个,所以我使用了我的Nexus 5X)
  5. 更多:

    我的设备同时安装了VLC播放器和Google相册,这些都可以播放MP4文件。作为证据,如果我将data更改为URL(链接到MP4文件),这将正常工作。

    这不是找不到文件的问题,因为我使用VideoView测试了相同的路径,并且它工作正常。

2 个答案:

答案 0 :(得分:1)

这种情况正在发生,因为您的设备或模拟器中没有可以处理mp4文件的应用程序。

为了避免崩溃,你可以做这样的事情

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;
}

答案 1 :(得分:1)

尝试这个想想它没有正确找到uri的东西:

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

然后打开让我选择照片或视频播放器。

但我认为这只是隐藏了它可以加载视频的事实,如SushiHangover所说。

是否有任何理由可以在其他活动中使用ViewVideo,如下所示:

 [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 ();
    }
}

布局

<?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>

修改

开头的'$'是c#6字符串插值https://msdn.microsoft.com/en-GB/library/dn961160.aspx

关于解决方案,我认为将视频文件放在应用程序的Raw文件夹中意味着任何意图都无法在应用程序的目录中播放它。所以最好的选择是如何将视频放入可播放的文件夹(将其复制到SD卡也不理想)或使用videoView播放视频