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.
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)
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)
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.
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
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.
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)
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.