Not only show you how to open pdf programmatically but also show you how to fix android.os.FileUriExposedException: exposed beyond app through Intent.getData() error.
Yesterday, I received an email from one of our readers, and he asked me to fix his issue where he was not able to open PDFs from the storage when the user clicked on the button.
And his application was running flawlessly on an older version of Android. After Android 11, his application was not working as per his plan.
I thought to resolve this, but they didn’t share the complete code, so I thought, why not create a demo application from scratch for our readers?
While developing the demo application to open PDF files, I got the following errors: “android.os.FileUriExposedException: file:///storage/emulated/0/TRENDOCEANS.pdf exposed beyond app through Intent.getData()” and “Couldn’t find meta-data for provider with authority”. If you also get this kind of error, then you can read our dedicated article on this issue.
Table of Contents
Open PDF Programmatically Using Intent
Before starting the process, let me walk through the concept of our demo application.
First and foremost, we will ask for permission, then the user will see a button that will invoke intent to open a pdf file that is already available in the root folder of the device, and that’s it.
Create a New Project in Android Studio
I’m not going to show you how to create a new project in Android Studio because you know how to do it very well.
Still, if you want, take a glimpse at how to create a new project in Android Studio, which is very similar to this project.
Create a Basic Layout in activity_main.xml
After creating a new project, the first thing you need to do is modify activity_main.xml to add a button that will invoke the openPDF() method, which we are going to create in the MainActivity file.
And the rest of the options are just for visual appeal, which you can skip for now.
Now you will modify AndroidManifest.xml to add <uses-permission></uses-permission> and <provider></provider> tags nested with the <meta></meta> tag.
The first <uses-permission/> tag will add the android:name=”android.permission.QUERY_ALL_PACKAGES” to list out the installed applications that can handle or read this pdf file.
To know more about this read package filtering.
And the last <uses-permission/> is necessary for the latest Android version.
If you are using Android 12, you will see two types of permission available compared to the previous Android version when you want to read files.
At the bottom, you need to add <provider/> tag, which is nested with <meta/> tag, to provide a location to access the storage directory.
At present, you cannot access any file directly. First, you need to add <provider/> <meta/> tag and specify the location in @xml/file_path.xml “Couldn’t find meta-data for provider with authority.”
Create a file_path in the XML Resource Path
The file @xml/file_path will be highlighted in red to indicate the missing file.
Simply, you just need to hover over @xml/file_path and click on Create XML Resource File” to create the missing file.
The next screen will ask you to enter the following details: After making the changes, click “OK” to move ahead.
And copy the below content to @xml/file_path to read the root directory of the external storage with the “external_file” name.
Modify MainActivity to Open PDF File in External Applications
First, you need to add a static constant value for permission check under the MainActivity class. For that, you can simply copy the below code and paste it into your editor.
Inside the onCreate method, you need to call verifyStoragePermission (this). After that, initialize the button to set a click listener.
Under the click listener, create a new method called openPDF ().
This verifyStoragePermission method is used to request and verify read permission.
The first step will be used to check the current status of permission. If it’s –1, then it will prompt you to accept the permission.
For Android 12, devices need extra permission to read all files, and that’s why we will use Intent to ask permission to read all file access permissions.
Inside openPDF(), you need to specify the location of the PDF file using the File class and save the value in a file variable, which you will use in uriPdfPath to declare the path; after that, using Intent, you can call an external app to read the PDF.
Earlier, we used “Uri path = Uri.fromFile(file);” to get the URI of a location, but it’s not working anymore, so you have to use “Uri uriPdfPath = FileProvider.getUriForFile()” and specify the .provider to declare the location you are going to access in @xml/file_path.xml.
And that’s a reason for the following error: “android.os.FileUriExposedException: file:///storage/emulated/0/TRENDOCEANS.pdf exposed beyond app through Intent.getData()”
Before running the application, please verify the code of MainActivity.
Run Application to Read PDF
Once the application is ready, install it on your device and test the functionality. Before that, keep the PDF in the specified location on your device.
That’s all to open PDF programmatically using Intent in Android.
Along with that, we have shown you the method to resolve “android.os.FileUriExposedException: file:///storage/emulated/0/TRENDOCEANS.pdf exposed beyond app through Intent.getData()” and “Couldn’t find meta-data for provider with authority”.
If you’re stuck somewhere, then let us know in the comment section.
A man with a tech effusive who has explored some of the amazing technology stuff and is exploring more. While moving towards, I had a chance to work on Android development, Linux, AWS, and DevOps with several open-source tools.