【问题标题】:How can I make my app visible to implicit intents to open a PDF file?如何使我的应用程序对打开 PDF 文件的隐式意图可见?
【发布时间】:2021-06-03 10:54:00
【问题描述】:

我想创建一个可以查看 PDF 文件的简单应用。

为了测试我是否删除了模拟器上的默认 PDF 文件查看器,当我想打开 PDF 文件时,模拟器会显示 Can't open that file

如何使我的应用程序对该隐式意图可见,以便在我单击该 PDF 文件时使用/显示它?
我需要在我的manifest.xml 中添加什么<intent-filter> 配置(如果有)?

【问题讨论】:

    标签: android android-intent android-manifest


    【解决方案1】:

    这对我有用,你没有指定你的 sdk 版本,但检查一下

    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:mimeType="*/*" android:host="*" android:pathPattern=".*.csv" />
    </intent-filter>
    

    【讨论】:

    • 当我这样做时告诉我Missing URL 错误。我指定了mimeType="application/pdf"pathPattern=".*.pdf"
    • 您可以使用 抑制警告
    【解决方案2】:

    @见javdromero's post

    在做了一些调整后,javdromero 的方法对我有用。

    1。指定 PDF mime 类型

    PDF 文件的 MIME 类型是 application/pdf,所以我们必须使用它:

    android:mimeType="application/pdf"
    

    android:host 属性可以忽略,因为我们不从网络获取任何内容,也没有指定android:scheme 属性。 android:pathPattern 属性也是如此。查看&lt;data&gt; 元素的documentation

    这给我们留下了一条错误消息Missing URL,并且文本是红色的。

    2。忽略缺少 URL 错误

    Missing URL 错误涉及整个 &lt;intent-filter&gt; 元素,因为我们应该在那里指定 android:scheme 属性。
    但是我们不使用任何方案,所以我们导入 tools 命名空间并指定 tools:ignore 属性:

    tools:ignore="AppLinkError"
    

    这给我们留下了以下&lt;intent-filter&gt;元素配置:

    <intent-filter tools:ignore="AppLinkUrlError">
        <action android:name="android.intent.action.VIEW"/>
    
        <category android:name="android.intent.category.DEFAULT"/>
    
        <data android:mimeType="application/pdf" />
    </intent-filter>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-06
      • 2016-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多