Android Question [SOLVED] pdf intent exception

mc73

Well-Known Member
Licensed User
Longtime User
I get this from a report:
B4X:
Exception android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///storage/emulated/0/Android/data/myapp.app/files/apdffile.pdf typ=application/pdf flg=0x20000}
I suspect that user doesn't have a pdf viewer. Should I just do a try while opening the intent activity, or there exists another check to do?
B4X:
Dim INTENT1 As Intent
       INTENT1.Initialize(INTENT1.ACTION_VIEW, "file://" & pdfFileName)
       INTENT1.SetType("application/pdf")
      ' should i add a try here?
try
      StartActivity(INTENT1)
catch
'no pdf viewer available
'inform user to install one
end try
 

mc73

Well-Known Member
Licensed User
Longtime User
Found it in the forum:
B4X:
Dim INTENT1 As Intent
       INTENT1.Initialize(INTENT1.ACTION_VIEW, "file://" & pdfFileName)
       INTENT1.SetType("application/pdf")
       Dim pdfAppsList As List
       pdfAppsList = QueryIntent(INTENT1)
       if pdfAppsList.size>0 then 
          ' pdf viewer(s) exists
      end if 


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("name")) 'return the activity full name
       listRes.Add(r.GetField("packageName"))
   Next
   Return listRes
End Sub
 
Upvote 0
Top