![android | Embarcadero RAD Studio Delphi C++Builder Blogs](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2020/08/android-1.jpg?resize=1140%2C628&ssl=1)
Intents on Android using API 26 to open PDF documents.
The Google Play store recently updated its requirements, so new apps were submitted with an API level of 26 as the target. While this was relatively simple to accomplish by updating the AndroidManifest.Template, the transition to the newer API altered the behavior of my application.
I used to download a file to the CachePath and then share it to a public folder before the update. I’d then obtain a URI for the public folder path file and share it using Intents. This no longer worked after the update. After doing some research, I discovered that this was due to changes in the Android security system, which make a lot of sense. Instead of sharing the file outside of the application, you now provide temporary access to it through the Intent. To accomplish this, you must first create a Provider (via XML) and then programmatically provide the path as a ‘content:/’ URI, set flags to allow read / write access via the intent, and share it.
In this post, we’ll show you a video that shows how to achieve this and demos the working code. To help, below are some of the XML blocks you will need along the way.
Adding Provider
Add this to the AndroidManifest.template in the source code root folder, before the </application> tag. This is then used to build all Android apps.
1 2 3 4 5 6 7 8 |
<provider android:name="android.support.v4.content.FileProvider" android:authorities="%package%.provider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/fileprovider" /> </provider> |
Provider file
Create a fileprovider.xml (or whatever file name you set in android:resource when declaring the provider).
1 2 3 4 |
<?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <cache-path name="bbresources" path="bbresources/"/> </paths> |
More flags and details for Provider Files can be found in the Android documentation
The post Opening a PDF on Android with Delphi appeared first on Stephen Ball’s Technical Blog.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition