Android Question Open PDF in External application

mfstuart

Active Member
Licensed User
Longtime User
Hi all,
I'm including a PDF "instructions" file as an asset in my application that will be opened by the user when they touch on a button on the app login screen.

The PDF was created with Word 2013, which I resaved from the Windows 10 PDF Viewer to make sure the PDF was in the correct format - incase that was an issue. So the PDF can be opened and viewed from Windows 10, no problem.
I can also open the PDF on the Nexus 7 tablet, finding the file with File Manager.

The code (which I found with a search on the forum) errors with "pdf cannot be opened".
Could someone please check the code out to see what causes the error? I cannot resolve it.

The button code is as follows:
B4X:
Sub btnHelp_Click
    Dim I1 As Intent
    'a folder created by the app, where the .sqlite db is also stored.
    Dim DBFolder As String = "sdcard/data/invscan/db"
    Dim PDF_Filename As String = "invscanhelp.pdf"
    Dim FullFilename As String = File.Combine(DBFolder, PDF_Filename)
   
    'Log(FullFilename)
   
    'copy Assets PDF file to DBFolder
    If File.Exists(File.DirAssets,PDF_Filename) = True Then
        If File.Exists(DBFolder,PDF_Filename) = False Then
            File.Copy(File.DirAssets,PDF_Filename,DBFolder,PDF_Filename)
        End If
    End If
   
    If File.Exists(DBFolder,PDF_Filename) = True Then
        I1.Initialize(I1.ACTION_VIEW, "file://" & FullFilename)
        'I1.Initialize(I1.ACTION_VIEW, File.Combine(DBFolder & "/", PDF_Filename))
        I1.SetComponent("android/com.android.internal.app.ResolverActivity")
        I1.SetType("application/pdf")
        I1.WrapAsIntentChooser("Choose PDF Viewer")
       
        StartActivity(I1)
    Else
        ToastMessageShow("Unable to find PDF (" & FullFilename & ")",False)
    End If   
End Sub

Thanx,
Mark S.
 

Star-Dust

Expert
Licensed User
Longtime User
I use this code:

B4X:
Dim ia As Intent
ia.Initialize(ia.ACTION_VIEW,"file://" &  File.Combine(path,FileName))
ia.SetType("application/pdf")
StartActivity(ia)

I have some doubt about the choice of the folder. Is not a private SDCARD / DATA folder?
 
Upvote 0

mfstuart

Active Member
Licensed User
Longtime User
I use this code:

B4X:
Dim ia As Intent
ia.Initialize(ia.ACTION_VIEW,"file://" &  File.Combine(path,FileName))
ia.SetType("application/pdf")
StartActivity(ia)

I have some doubt about the choice of the folder. Is not a private SDCARD / DATA folder?

Yes, the folder was created by the app as a "working" folder. Is this why the PDF reader cannot open the file?

The .sqlite db is in this folder and doing CRUD to the db is no problem. So the PDF should also be no problem, I would suppose.
I remember a post from Erel mentioning the PDF can not be opened from the Asset folder, so hence me copying the file to another folder to open it.

Thanx Star-Dust.
 
Upvote 0

mfstuart

Active Member
Licensed User
Longtime User
I use this code:

B4X:
Dim ia As Intent
ia.Initialize(ia.ACTION_VIEW,"file://" &  File.Combine(path,FileName))
ia.SetType("application/pdf")
StartActivity(ia)

I have some doubt about the choice of the folder. Is not a private SDCARD / DATA folder?
Your doubt was what made me change my code.
I changed it to File.DirDefaultExternal as the target folder to copy the PDF to, as in:
B4X:
Sub btnHelp_Click
    Dim I1 As Intent
    Dim PDF_Filename As String = "invscanhelp.pdf"
   
    'copy Assets PDF file to DirDefaultExternal folder
    If File.Exists(File.DirAssets,PDF_Filename) = True Then
        File.Copy(File.DirAssets,PDF_Filename,File.DirDefaultExternal,PDF_Filename)
    End If
   
    If File.Exists(File.DirDefaultExternal,PDF_Filename) = True Then
        I1.Initialize(I1.ACTION_VIEW, "file://" & File.DirDefaultExternal & "/" & PDF_Filename)
        I1.SetType("application/pdf")
        I1.WrapAsIntentChooser("Choose PDF Viewer")
        StartActivity(I1)
    Else
        ToastMessageShow("PDF not found.",False)
    End If   
End Sub
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
If it was a private folder for the app, an external program to read PDF files could not access, so it would create an error
 
Upvote 0
Top