Site icon Embarcadero RAD Studio, Delphi, & C++Builder Blogs

Opening a PDF on Android with Delphi

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.

[crayon-6762d19a06d4e505383966/]

Provider file

Create a fileprovider.xml (or whatever file name you set in android:resource when declaring the provider).

[crayon-6762d19a06d56620276435/]

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.

Do you want to build an Android app? Try the Android App Builder Software, which will assist you in developing apps in the Delphi or C++ environments.
Exit mobile version