Android Question No pdf viewer found in some users' phones

mc73

Well-Known Member
Licensed User
Longtime User
In an app, I'm searching if at least one pdf viewer is installed, in order to open pdf files there.

In order to do that I use:

B4X:
Dim INTENT1 As Intent
INTENT1.Initialize(INTENT1.ACTION_VIEW,pdfFileName)
INTENT1.SetType("application/pdf")
INTENT1.Flags=1
'INTENT1.SetComponent("android/com.android.internal.app.ResolverActivity")
'I can't remember why I remarked the above line, but it's been ages since then      
Dim pdfAppsList As List
pdfAppsList = QueryIntent(INTENT1)
If pdfAppsList.IsInitialized=True Then 
    If pdfAppsList.Size>0 Then 
        StartActivity(INTENT1)
    end if
end if

And the queryIntent sub is:

B4X:
Sub QueryIntent(Intent1 As Intent) As List
    Dim r As Reflector
    r.Target = r.GetContext
    r.Target = r.RunMethod("getPackageManager")
    Dim list1 As List
    list1 = r.RunMethod4("queryIntentActivities", Array As Object(Intent1, 0), Array As String("android.content.Intent", "java.lang.int"))
    Dim listRes As List
    listRes.Initialize
    For i = 0 To list1.Size - 1
        r.Target = list1.Get(i)
        r.Target = r.GetField("activityInfo")
        listRes.Add(r.GetField("packageName"))
    Next
    Return listRes
End Sub

I never experienced any problem with this coding, not until now that some users complain about my app not recognizing their installed pdf viewer.

In my devices for testing, I coudln't find a problem, so I'm wondering if any of you experienced such thing and (or not) found a way to solve it.

Is it a problem with my code, for e.g. some new weird permissions?
 

mc73

Well-Known Member
Licensed User
Longtime User
1. Your code will not work on new devices. You must use FileProvider.

2. It is better and simpler to call StartActivity and catch the error:
B4X:
Try
 StartActivity(Intent)
Catch
 Log("no matching application")
End Try
Hi Erel,

I will surely try the second one, I cannot even remember why I had the previous code, but it seems like I didn't get an error if no viewer was there, who knows? :)

In step 1 you're describing, I thought I've already done that since my code for the pdfFile goes like this:
B4X:
Dim pdfFileName As String
pdfFileName=Starter.Provider.GetFileUri(fileName)
where I have
B4X:
Public Provider As FileProvider
in the Starter module. Should I do something extra on this one?
 
Upvote 0
Top