【问题标题】:Android Implicit Intent for Viewing a Video File用于查看视频文件的 Android 隐式 Intent
【发布时间】:2015-06-22 02:59:02
【问题描述】:

在我的 Android 应用中,我有一个按钮,单击该按钮会启动我选择的外部应用程序来播放视频(我认为这称为“隐式意图”)。这是我的onCreate 方法中的相关Java 代码。

Button button = (Button) findViewById(R.id.button);

button.setOnClickListener
(
    new Button.OnClickListener()
    {
        public void onClick(View v)
        {
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setDataAndType(Uri.parse("https://youtu.be/jxoG_Y6dvU8"), "video/*");
            startActivity(i);
         }
    }
);

我预计这会起作用,因为我已经非常密切地遵循教程和 Android 开发人员文档,但是当我在 AVD 中测试我的应用程序时,而不是提示我可以查看我的视频的外部应用程序菜单,应用程序崩溃。

是什么导致我的应用崩溃?

【问题讨论】:

  • 请发布您的日志文件!
  • @GaneshThiagarajan Android Studio 新手...我该怎么做?
  • 您应该会看到类似 LogCat 的内容。那是我们通常会收到错误代码和消息的地方。
  • 这可能与https://youtu.be/jxoG_Y6dvU8 实际上是一个text/html 文件有关。

标签: java android android-intent uri avd


【解决方案1】:

将您的 onClick 方法更改为以下代码。您应该提供选择外部播放器的选项。

@Override
public void onClick(View v) {

        Intent intent = new Intent(Intent.ACTION_VIEW);

        intent.setDataAndType(Uri.parse("https://youtu.be/jxoG_Y6dvU8"), "video/*");

        startActivity(Intent.createChooser(intent, "Complete action using"));


}

【讨论】:

    【解决方案2】:

    更改您的代码以添加此检查:

            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setDataAndType(Uri.parse("https://youtu.be/jxoG_Y6dvU8"), "video/*");
    
            // Check there is an activity that can handle this intent
            if (i.resolveActivity(getPackageManager()) == null) {
                // TODO No activity available. Do something else.
            } else {
                startActivity(i);
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多