【问题标题】:How to receive pdf file from intent filter - Android如何从意图过滤器接收 pdf 文件 - Android
【发布时间】:2021-11-30 01:11:47
【问题描述】:

我已经构建了一个 PDF 阅读器,我想从手机 Ex 的任何地方打开 pdf。 Whatsapp,文件管理器。

当我从谷歌文件管理器打开 pdf 时它正在工作,当我从默认文件管理器打开它时它会崩溃

显示此错误

     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.os.Bundle.get(java.lang.String)' on a null object reference

应用清单文件

        <activity
        android:name=".PDF_Viewer"
        android:exported="true">

        <intent-filter>

            <action
                android:name="android.intent.action.VIEW" />

            <category
                android:name="android.intent.category.DEFAULT" />

            <data
                android:mimeType="application/pdf" />

        </intent-filter>

        <intent-filter>

            <action
                android:name="android.intent.action.VIEW" />

            <category
                android:name="android.intent.category.DEFAULT" />

            <data
                android:scheme="file"
                android:host="*"
                android:pathPattern=".*\.pdf" />
        </intent-filter>

        <intent-filter>

            <action
                android:name="android.intent.action.VIEW" />

            <category
                android:name="android.intent.category.DEFAULT" />

            <category
                android:name="android.intent.category.BROWSABLE" />

            <data
                android:scheme="http"
                android:host="*"
                android:pathPattern=".*\.pdf" />

            <data
                android:scheme="https"
                android:host="*"
                android:pathPattern=".*\.pdf" />
        </intent-filter>

    </activity>

我在 PDF 查看器活动中收到这样的意图:

Intent intent = getIntent();
Bundle bundle = intent.getExtras();
Uri pdf = (Uri) bundle.get(Intent.EXTRA_STREAM);

【问题讨论】:

  • 发布更多代码,因为这个包来自某个地方。您的 NullPointerException 告诉 bundle==null。使用前检查是否为空。
  • 我添加了更多代码,请检查
  • 您没有在使用前添加代码来检查是否为空。此外,您没有提供额外信息。你现在会做什么 bundle==null ?
  • 我不会在使用前检查捆绑包是否为空,如果您知道如何修复它,请帮助 -@blackapps
  • 使用前检查bundle是否为空!添加代码!我第三次问了。

标签: android pdf android-intent android-pendingintent intentfilter


【解决方案1】:

您的所有三个&lt;intent-filters&gt; 都用于ACTION_VIEWThe documentation for ACTION_VIEW 有:

输入:getData() 是从中检索数据的 URI。

因此,考虑到这一点,请将您的 Java 代码替换为:

Intent intent = getIntent();
Uri pdf = intent.getData();

您可以考虑检查现有的 PDF 查看器应用程序以了解它们的工作原理。比如many open source ones listed on F-Droid

【讨论】:

    猜你喜欢
    • 2016-11-06
    • 1970-01-01
    • 1970-01-01
    • 2015-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-06
    相关资源
    最近更新 更多