How to set app default for opening pdf in Android

683 Views Asked by At

I have build an PDF Reader and i want to open my app to open PDF's from any where on device

Like:-

Whatsapp or File Manager

I want to set my app default for opening PDF's

Help me please I'm searching it for past 3 hours

1

There are 1 best solutions below

0
Dahbi Oussama On

Try to use this code in you Manifest.xml :

<activity  android:name=".MainActivity"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.MULTIWINDOW_LAUNCHER"/>
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="file" />
            <data android:mimeType="application/pdf" />
            <data android:pathPattern=".*\\.pdf" />
            <data android:host="*" />
        </intent-filter>
    </activity>

and in your MainActivity.java :

 Intent intent = getIntent();
Uri uri;

pdfView = findViewById(R.id.pdfViewer);

if (Intent.ACTION_VIEW.equals(intent.getAction())) {
    uri = intent.getData();
    pdfView.fromUri(uri).load();
}

if (Intent.ACTION_SEND.equals(intent.getAction())) {
   uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
   pdfView.fromUri(uri).load();
}