Android Question Problem viewing PDF file on Android 9.0

anglia

Member
Licensed User
Longtime User
I've developed a simple app that creates a PDF file which is then saved to a folder.

The app then allows the user to view the PDF using the following intent-


dim xintent as intent
Dim zpathfile As String
zpathfile="file://" & PDFfolder & "/" & "pdf1.pdf"
xintent.Initialize(xintent.ACTION_VIEW,zpathfile)
xintent.SetType("application/pdf")
xintent.WrapAsIntentChooser("Choose PDF Viewer")
StartActivity(xintent)


It works fine on my Android 5.1.1 tablet and on my Android 6.0 phone.
My friend also wants to use the app but there's a problem when he runs it on his Android 9.0 phone.

The PDF file is created and stored okay but the app crashes when he tries to view the PDF file.
I've looked at trying to use Filepicker, Fileprovider and think there must be some issue with runtime permissions.
I'm totally lost!

The app is for our use only and will not need to conform to Google Play requirements.

Could someone please point me in the right direction so the app will work on the Android 9.0 device?

Thanks
 

DonManfred

Expert
Licensed User
Longtime User
Please use [CODE]code here...[/CODE] tags when posting code.




zpathfile="file://" & PDFfolder & "/" & "pdf1.pdf"
This will not work on higher Android without using fileProvider!

I've looked at trying to use Filepicker, Fileprovider and think there must be some issue with runtime permissions.
No.

Check this Thread carefully. There are some new requirements when using targetsdk 28.

android.jar / targetSdkVersion / minSdkVersion

Especially you need to use FileProvider to share a file with another app.

This code snippet may help you (note to implement FileProvider correctly first).
B4X:
Dim FileName As String = "xx.pdf"
        File.Copy(File.DirAssets, FileName, Starter.Provider.SharedFolder, FileName)
        Dim in As Intent
        in.Initialize(in.ACTION_VIEW, "")
        Starter.Provider.SetFileUriAsIntentData(in, FileName)
        'Type must be set after calling SetFileUriAsIntentData
        in.SetComponent("android/com.android.internal.app.ResolverActivity")
        in.SetType("application/pdf")
        StartActivity(in)
 
Last edited:
Upvote 0

anglia

Member
Licensed User
Longtime User
Many thanks DonManfred.

As the app will only be used by myself and friend who has Android 9.0 on his phone, will setting targetsdk to 23 be an option?
 
Upvote 0

anglia

Member
Licensed User
Longtime User
Thanks again DonManfred.

That's good news I'll set targetsdk to 23.

However, I will try and get an understanding of FileProvider for future projects.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…