【问题标题】:No Activity found to handle Intent whien trying to open word file or excel file尝试打开 word 文件或 excel 文件时未找到处理 Intent 的活动
【发布时间】:2021-11-17 18:20:33
【问题描述】:

"这里我要打开文件"

val intent = Intent()
intent.action = Intent.ACTION_VIEW
intent.setDataAndType(Uri.parse(docList[position]?.image), docList[position]?.contentType)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
startActivity(holder.itemView.context, intent, null)

【问题讨论】:

  • val intent = Intent() intent.action = Intent.ACTION_VIEW intent.setDataAndType(Uri.parse(docList[position]?.image), docList[position]?.contentType) intent.addFlags( Intent.FLAG_GRANT_READ_URI_PERMISSION) startActivity(holder.itemView.context, intent, null)
  • 检查我上面的代码
  • 请将您的代码发布在普通代码块中。并再次使用代码删除该注释。
  • 发布Uri 的示例-您在docList 中引用了一个名为.image 的字段似乎很奇怪?并发布contentType
  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: java android kotlin android-intent


【解决方案1】:

如果您收到 ActivityNotFound 异常,则表示您的设备中没有应用程序支持此文件类型。

首先将您的代码包装在 try/catch 块中以防止崩溃。

那么你有一些选择:

第一种方式:你可以打开Play商店提示用户下载办公应用

try{
        val intent = Intent()
        intent.action = Intent.ACTION_VIEW
        intent.setDataAndType(Uri.parse(docList[position]?.image),
                docList[position]?.contentType)
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
        startActivity(holder.itemView.context, intent, null)
    }
        catch(e:ActivityNotFoundException){

            //NOTE: you can show toast to user or open google play store for required app

            startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=com.microsoft.office.officehubrow")))

        }

第二种方式:您可以使用android WebView在您的应用中显示excel / word文件类型而不需要Intent

检查一下:

How to open Excel, .doc files in Webview in Android?

【讨论】:

    【解决方案2】:
    val intent = Intent()
        intent.action = Intent.ACTION_VIEW
        intent.setDataAndType(Uri.parse(docList[position]?.image), docList[position]?.contentType)
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
        if (intent.resolveActivity(requireActivity().packageManager) != null) {
            startActivity(holder.itemView.context, intent, null)
        } else {
            Toast.makeText(requireActivity(), "No Application available to handle this intent", Toast.LENGTH_LONG).show()
        }
    

    使用意图的resolveActivity方法。它将返回可以处理此意图的应用程序,否则它将为空。

    【讨论】:

      猜你喜欢
      • 2014-11-14
      • 1970-01-01
      • 1970-01-01
      • 2017-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-29
      • 1970-01-01
      相关资源
      最近更新 更多