Android Question Opening pdf file

microbox

Active Member
Licensed User
Longtime User
Hi everyone, I am not able to make the following code to open a specific file.
B4X:
BytesToFile(File.DirDefaultExternal & "/" & AppName,vFileName,mm.Image)
chatroompage.chat1.AddItem(mm.Name & CRLF & vFileName,False)
Log("-----------Done-------------")
'ToastMessageShow("Download complete!",False)
Msgbox2Async("Do you wantto open this file?", "Download complete", "Yes", "Cancel", "", Null, False)
Wait For Msgbox_Result (Result As Int)
If Result = DialogResponse.POSITIVE Then
Log("Yes")
Dim i As Intent
i.Initialize(i.ACTION_VIEW,File.DirDefaultExternal & "/" & AppName &"/"& vFileName)
i.SetType("application/pdf")
StartActivity(i)
End If
 

Robert Valentino

Well-Known Member
Licensed User
Longtime User
I believe you should set
B4X:
i.Flags = 1
And the file must be in a SHARED location, you should use FileProvider because you need to pass a URI
I've attached a modified version of Erel's FileProvider that I am using
B4X:
                        Dim xFileProvider As FileProvider

                        xFileProvider.Initialize

                        File.Copy(File.DireDefaultExternal, & "/" & AppName &"/"& vFileName, xFileProvider.gSharedFolder, FileName)

                        Dim ViewIntent As Intent

                        ViewIntent.Initialize(ViewIntent.ACTION_VIEW, xFileProvider.GetFileUri(FileName)) 

                        ViewIntent.Flags     = 1
                        ViewIntent.SetType("application/pdf")

                        StartActivity(ViewIntent)

You will need something like this in your 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" />
)

Hope this helps
 

Attachments

  • FileProvider.bas
    3.2 KB · Views: 235
Upvote 0
Top