Android Question Need File Provider Help. Can't Open PDF

cbanks

Active Member
Licensed User
Longtime User
This code has worked fine to open a PDF:

B4X:
Dim i As Intent
Dim uri As String
uri = "file://" & File.Combine(rp.GetSafeDirDefaultExternal(""), pdfurl)
i.Initialize(i.ACTION_VIEW, uri)
i.SetType("application/pdf")
i.WrapAsIntentChooser("Choose PDF Viewer")
StartActivity(i)

Now it just gives an error message. I know I need to convert this code to use File Provider and I've followed the instructions but can't get it to work. Just crashes app. Please help with code conversion. Thanks.
 

Almora

Active Member
Licensed User
Longtime User
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)
        in.SetComponent("android/com.android.internal.app.ResolverActivity")
        in.SetType("application/pdf")
        StartActivity(in)
 
Upvote 0

cbanks

Active Member
Licensed User
Longtime User
I've added the class and the necessary libraries. I've added what I needed to the manifest editor. I added the service to initialize the FileProvider.

I'm just not sure how to change my code I posted so that it will open a PDF. I've tried making the necessary changes but it just crashes the app.

I've tried something like this but it just crashes the app:

B4X:
Dim uri As String = "file://" & File.Combine(rp.GetSafeDirDefaultExternal(""), pdfurl)
Dim i As Intent
i.Initialize(i.ACTION_VIEW, uri)
Starter.Provider.SetFileUriAsIntentData(i, uri)
i.SetType("application/pdf")
i.WrapAsIntentChooser("Choose PDF Viewer")
StartActivity(i)
 
Last edited:
Upvote 0

cbanks

Active Member
Licensed User
Longtime User
I did try to copy the code from the btnViewImage_Click example but it doesn't work. I dont' know what I'm doing wrong.
 
Upvote 0

cbanks

Active Member
Licensed User
Longtime User
B4X:
Dim uri As String = "file://" & File.Combine(rp.GetSafeDirDefaultExternal(""), pdfurl)
Dim i As Intent
i.Initialize(i.ACTION_VIEW, uri)
Starter.Provider.SetFileUriAsIntentData(i, uri)
i.SetType("application/pdf")
i.WrapAsIntentChooser("Choose PDF Viewer")
StartActivity(i)

The app crashes and depending how I've modified the code sometimes after the app crashes it will then prompt to open the file in a PDF viewer but then says it can't access the pdf file.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I did try to copy the code from the btnViewImage_Click example but it doesn't work.
Why are you not copying all of them?

B4X:
Sub btnViewImage_Click
    Dim FileName As String = "b4a.png"
    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.SetType("image/*")
    StartActivity(in)
End Sub
The file is copied to the sharedfolder and afterwards the fileprovider is used to generate the uri in the intent.
The intent in initialized with an Empty Uri

Dim uri As String = "file://" & File.Combine(rp.GetSafeDirDefaultExternal(""), pdfurl)
Dim i As Intent
i.Initialize(i.ACTION_VIEW, uri)
Starter.Provider.SetFileUriAsIntentData(i,
uri)
i.SetType(
"application/pdf")
i.WrapAsIntentChooser(
"Choose PDF Viewer")
StartActivity(i)

You are NOT copying the file to the shared folder.
You are using your OWN Uri (mistake)
You are initialising the Intent wrong.
You are using the Uri instead of the filename in the SetFileUriAsIntentData.

I suggest to start again implementing it the correct way.
 
Last edited:
Upvote 0

cbanks

Active Member
Licensed User
Longtime User
I tried this code and the app crashes but it actually then opens the PDF successfully! Any idea why the app still crashes though? What version of B4A is required for File.Provider?

B4X:
Dim pdfurlindex As Int = pdfurl.LastIndexOf("/") + 1
Dim FileName As String = pdfurl.SubString(pdfurlindex)
File.Copy(rp.GetSafeDirDefaultExternal("") & pdfurl.SubString2(0, pdfurlindex), FileName, Starter.Provider.SharedFolder, FileName)
Dim in As Intent
in.Initialize(in.ACTION_VIEW, "")
Starter.Provider.SetFileUriAsIntentData(in, FileName)
in.SetType("application/pdf")
StartActivity(in)
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Any idea why the app still crashes though?
HOW should i know? You did not post any error you got. I can not guess it.

What version of B4A is required for File.Provider?
What version are you using?

PD: If you are using an outdated B4A Version then this is something that you should have written in #1 to inform us about. In some cases it is mandatory to know such info.
 
Last edited:
Upvote 0

cbanks

Active Member
Licensed User
Longtime User
Got it all working! Thanks for your help! Found a problem in Activity_Pause that was causing the app to crash when opening the PDF.
 
Upvote 0
Top