Step-by-Step Guide to Making Your Android App the Default PDF Reader in Android Studio

For the code-aholics that are not read-aholics here’s a TLDR.

1. In the manifest file, add an intent filter to the activity tag for the activity that contains your pdf reader

2. In your pdf activity or composable retrieve the uri from the intent

In Detail

The goal here is to make sure your app appears in the list of apps that can be used to open pdf files when a user taps a pdf document anywhere on the device. In order to do that, you have to inform the android system that you have an activity that can handle certain kinds of intents. and in our case, intents originating from other apps or your app, that request to open pdf files. This can be achieved by adding an intent filter in the manifest file, to the activity in your app that can handle opening of pdf files. Below is some sample code to do that.

<activity
android:name=".MainActivity"
android:exported="true"
android:theme="@style/Theme.AppTheme">
<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.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="file" />
<data android:scheme="content" />
<data android:mimeType="application/pdf" />
</intent-filter>

</activity>

Here’s a breakdown of what each of the tags stands for and what it implies so that you can be able to debug and troubleshoot issues that may arise.

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

This tag specifies the action that the activity can perform, In this case, the action is `VIEW`, which means the activity can display some data to the user.

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

These tags provide the system with additional information on how the activity should be launched.

`DEFAULT` is necessary for your activity to be considered for implicit intents, which do not specify to the android system what exact activity or component should handle the intent. Without it, your activity can only be launched by intents that explicitly specify its component name.
`BROWSABLE` can be specified if your activity can be invoked from a web browser to open certain web URLs that lead directly to pdf documents.

Data tags<data android: scheme="file" />
<data android: scheme="content" />
<data android: scheme="http" />
<data android: scheme="https" />
<data android: mimeType="application/pdf" />

Data tags are used to specify the type of data the activity can handle.
In the above example we have specified that we can handle 4 Uri schemes, the file scheme (file://host/path) the content scheme (content://user_dictionary/words), the http Uri scheme (http://domain/path) and the https Uri scheme (https://domain/path) and that the MIME type for these must be `application/pdf`, which is the standard MIME type for PDF files.

With this in place, the android system now knows that activity can handle VIEW actions for PDF files, whether they are accessed via the file system, content providers or from the web. When an intent that matches these criteria is fired, your app will be listed as an option for handling it, and if you’re the only app or the user sets it as default, your app will open these links/files without prompting the user.

So how do we then handle this request to view pdf files?

It’s very simple, you will just need to observe the intent that started your android activity’s onCreate method and extract the data that was delivered to your activity through it like so.

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

// This will hold the navigation start destination
var startDestination = "home"

// Check for the action and type of intent
if (intent?.action == Intent.ACTION_VIEW && intent.type != null) {
if ("application/pdf" == intent.type) {
// If the intent is a PDF, we'll start with the PDF viewer destination
startDestination = "pdfViewer"
}
}

setContent {
YourAppTheme {
// Use the startDestination from above to set the start destination
AppNavigation(startDestination = startDestination)
}
}
}

In jetpack compose, you can do something like this when you get to your pdfViewerval context = LocalContext.current
val activity = context.findActivity()
val intent = activity?.intent
val uri = intent?.data
uri?.let {
vm.loadIntentPdf(it, user)
}

findActivity() is an extension function I wrote to unwrap the context in case its a context wrapperfun Context.findActivity(): Activity? = when (this) {
is Activity -> this
is ContextWrapper -> baseContext.findActivity()
else -> null
}

Well, there you have it. we’ve now set the stage for your Android app to become the go-to PDF reader. It will pop up when users tap on a PDF. Here’s a toast to your app’s successful journey to the top of the PDF reading stack for PDF enthusiasts!

Sharif

Writer & Blogger

Join Our Community Now

Follow us on;

We give quality services
OUR PAGES
Home
Services
Contact Us
About Us
Team
Portfolio
OUR SERVICES
Mobile App Development
Website Design and Development
Social Media Marketing
Content Creation
AI and Marketing Analytics
Document Revision and Translation
OUR PARTNERS
Artsy Africa
International University of Applied Sciences
Human Intelligence International
International Experts
Trainers 4 Training
Clewa-Projects
Hoteliers Guild
Dilis Fruits
Add-On
Hairstylist Sarah
Kyeyune Janet
Reach out
Our Offices

Kampala, KIbuli, Uganda

follow us on;

© Copyright 2023 @artsyafricaremotesquad