Home > Net >  How to receive pdf file from intent filter - Android
How to receive pdf file from intent filter - Android

Time:10-11

I have build a PDF Reader which i want to open pdf from any where from phone Ex. Whatsapp, File Manager.

It is working when i open a pdf from google file manager and when i open it from default file manager it crashes

shows this error

     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

App Manifest File

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

I am receiving intent in PDF Viewer Activity like this:

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

CodePudding user response:

All three of your <intent-filters> are for ACTION_VIEW. The documentation for ACTION_VIEW has:

Input: getData() is URI from which to retrieve data.

So, with that in mind, replace your Java code with:

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

You might consider examining existing PDF viewer apps to see how they work. There are many open source ones listed on F-Droid, for example.

  • Related