Android Question Showing a PDF File with dangerous permission.

Omar Moreno

Member
Licensed User
Longtime User
Hello everyone.

I have B4X: 12.50 with OpenJDK 14
I have created 3 emulators: Api 28, Api 31 and Api 33
I have 4 cell phones: Api 23, Api 31 and the other two Api 33

In the File.DirAssets folder I have a PDF file that I send to Provider.SharedFolder with File.CopyAsync
The code to display the PDF file with Intent is:
B4X:
Dim in As Intent
in.Initialize(in.ACTION_VIEW, "")
Provider.SetFileUriAsIntentData(in,  "FilePDF.pdf")
in.SetType("application/pdf")
StartActivity(in)
The previous code works very well on all 4 cell phones, but on emulators it only works on Api 33.
To make it work on emulators: Api 28, Api 31, I had to add the code

in.SetComponent("android/com.android.internal.app.ResolverActivity")

The emulators do not send an error, they only warn that: “no apps can perform this action”
But that line of code doesn't work with Api 33 emulator, so I had to do this:
B4X:
Dim in As Intent
in.Initialize(in.ACTION_VIEW, "")
Provider.SetFileUriAsIntentData(in, ArchPdf)
If VersionSDK < 33 Then
    in.SetComponent("android/com.android.internal.app.ResolverActivity") '<---
End If
in.SetType("application/pdf")
StartActivity(in)
To get the SDK version use the Phone library with the dangerous permission PERMISSION_READ_PHONE_STATE

The questions are:
1. Is it really necessary to validate the SDK version of the device, because it is only required by emulators (SDK < 33) to use the in.SetComponent code.?
2. If the answer to question 1 is true then will I have problems with the Play Store if I publish the APK?

Thanks for your possible answers.
 
Solution
You don't need this line.

You should add put the StartActivity call inside a Try / Catch block as not all devices will have an app installed that can handle pdf.
Top