Android Question FileProvider and Opening PDF Application List

tsteward

Well-Known Member
Licensed User
Longtime User
My App downloads a PDF then using FileProvider it uses share to open the PDF. But the list applications offered does not include any pdf viewers which are installed on the device.

How can I show the PDF apps to select.

I have added to my Manifest
B4X:
AddApplicationText(
  <provider
  android:name="android.support.v4.content.FileProvider"
  android:authorities="$PACKAGE$.provider"
  android:exported="false"
  android:grantUriPermissions="true">
  <meta-data
  android:name="android.support.FILE_PROVIDER_PATHS"
  android:resource="@xml/provider_paths"/>
  </provider>
)
CreateResource(xml, provider_paths,
   <files-path name="name" path="shared" />
)

Then my app is as follows
B4X:
If job1.Success Then
                       
                    Dim out As OutputStream = File.OpenOutput(Starter.Provider.SharedFolder, fname, False)
                    File.Copy2(job1.GetInputStream, out)
                    out.Close
                    ProgressDialogHide
                    If foMenu.IsInitialized Then
                        If foMenu.MenuIsVisible Then
                            foMenu.HideFan
                        End If
                    End If
                    Log("file://" & Starter.Provider.SharedFolder & "/" & fname)
                    Dim in As Intent
                    in.Initialize(in.ACTION_SEND, "")
                    in.SetType("application/pdf")
                    in.PutExtra("android.intent.extra.STREAM", Starter.Provider.GetFileUri(fname))
                    in.Flags = 1 'FLAG_GRANT_READ_URI_PERMISSION
                    StartActivity(in)
                End If

When executed I get the list below to choose from no pdf applications in list.
 

Attachments

  • Screenshot_20190507-195002_Android System.jpg
    112.5 KB · Views: 503

Almora

Well-Known Member
Licensed User
Longtime User
this can help.

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)
        'Type must be set after calling SetFileUriAsIntentData
        in.SetComponent("android/com.android.internal.app.ResolverActivity")
        in.SetType("application/pdf")
        StartActivity(in)
 
Upvote 0

tsteward

Well-Known Member
Licensed User
Longtime User
Thats perfect thank you!!!
 
Upvote 0

mfstuart

Active Member
Licensed User
Longtime User
Thats perfect thank you!!!
Hello,
I'm trying to do the same thing that you have done, except my app does not download the files from a server like you did, but DropBox.
Any chance of sharing what the solution is on opening a file (pdf, doc, or docx) from the device itself?

My app downloads the files (song charts) from another persons Dropbox (which I have access to) and they are saved/copied manually to a folder I created on the tablet device.
From there the filenames are displayed in a ListView.
Touching on a ListView row gets the filename, which is used in code to try and open and view the file.
I've tried a few things but none have opened the file.

Would sure appreciate you sharing something that would work for me.

Regards,
Mark Stuart
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
it is as above, except you don't need the parts that refer to the download "job".
you already have the file; just copy it to your fileprovider like member almora did
(from wherever it is that you have it stored).
 
Upvote 0

mfstuart

Active Member
Licensed User
Longtime User
Hi,
It worked. This is what I did...
Built a new project..
1. Selected a PDF file and added it to the project "MyFile.pdf" (in the Files section)
2. Added the above ApplicationText to the Manifest, from post #1.
3. Added a button to the layout - btnOpenFile
4. Selected the FileProvider library
5. Added this to the Process_Globals - Private Provider As FileProvider
6. Initialized the variable in the Activity_Create - Provider.Initialize
7. and added the following code for the button...
B4X:
Private Sub btnOpenFile_Click
    Dim Filename As String = "MyFile.pdf"
    File.Copy(File.DirAssets,Filename,Provider.SharedFolder,Filename)
    Dim in As Intent
    in.Initialize(in.ACTION_VIEW,"")
    Provider.SetFileUriAsIntentData(in,Filename)
    'in.SetComponent("android/com.android.internal.app.ResolverActivity")    'you can uncomment this line for the user to select which app will open and view the PDF file
    in.SetType("application/pdf")
    StartActivity(in)
End Sub

Now I just got to get it to work for viewing files NOT in the File.DirAssests, but for files already on the device.

Thanx to all here for helping.
Mark Stuart
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…