Android Question Not able to open downloaded pdf file in Starter.Provider.SharedFolder

Mahimahi

Member
Licensed User
On my test phone running Android 8, a "Moto g6 play", I am not able to open the downloaded pdf file for some reasons. I've added fileProvider class, also attempted to check and request "PERMISSION_WRITE_EXTERNAL_STORAGE". I am able to see the inputstream copied to the phone internal storage [internal storage]/Android/data/com.abcd.doc.updater/files/shared/ but I am not able to open the pdf file.

B4X:
Sub JobDone (Job As HttpJob)
   Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
   If Job.Success = True Then
       Select Job.JobName
           Case "Job1"
               customPdfFilenameToSave = "abcd.pdf"

               Dim OutStream As OutputStream
               OutStream = File.OpenOutput(FindUserDownloadsFolder, customPdfFilenameToSave, False)
                                              
               File.Copy2(Job.GetInputStream,OutStream)
               OutStream.Close
              
               OpenPDF2(File.Combine(FindUserDownloadsFolder, customPdfFilenameToSave))
       End Select
   Else
       Log("Error: " & Job.ErrorMessage)
   End If
   Job.Release
End Sub
              
Sub OpenPDF2(FileName As String)
   Dim in As Intent
   in.Initialize(in.ACTION_VIEW, "")
   Starter.Provider.SetFileUriAsIntentData(in, FileName)
   'Type must be set after calling SetFileUriAsIntentData
   in.SetType("application/pdf")
   StartActivity(in)
End Sub

Sub FindUserDownloadsFolder As String
       Return Starter.rp.GetSafeDirDefaultExternal("shared")
       'Return Starter.Provider.SharedFolder
End Sub

Please advise. Thanks a lot.
 

Attachments

  • Screenshot_20190318-162434.png
    Screenshot_20190318-162434.png
    325.2 KB · Views: 146

Mahimahi

Member
Licensed User
Thanks DonManfred. I am not trying to duplicate threads. I interpreted Erel's very last message this issue with Android 8.0 is a separate issue and should create a new thread, thus I am doing that.

I am using, or trying to use Fileprovider, although it could be that certain things are not done correctly, otherwise it should work, correct? I have no issue now writing the inputstream to Starter.rp.GetSafeDirDefaultExternal("shared"), but if trying to write the inputstream to Starter.Provider.SharedFolder, it would not write the pdf.

The question now is with Android 8.0 I am not able to open the saved pdf. You will find that in my code above, with sub OpenPDF2 I am trying to use FileProvider to open the saved pdf. Please help elaborate how to solve the problem. Thanks.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Sub FindUserDownloadsFolder As String
Return Starter.rp.GetSafeDirDefaultExternal("shared")
'Return Starter.Provider.SharedFolder
End Sub
1. https://www.b4x.com/android/forum/threads/class-fileprovider-share-files.97865/#content
2.
B4X:
Public Sub Initialize
    Dim p As Phone
    If p.SdkVersion >= 24 Or File.ExternalWritable = False Then
        UseFileProvider = True
        SharedFolder = File.Combine(File.DirInternal, "shared")
        File.MakeDir("", SharedFolder)
    Else
        UseFileProvider = False
        SharedFolder = rp.GetSafeDirDefaultExternal("shared")
    End If
    Log($"Using FileProvider? ${UseFileProvider}"$)
End Sub
Your FindUserDownloadsFolder returns another folder than the Fileproviderclass is using. At least on SDK >=24.

B4X:
Dim OutStream As OutputStream
               OutStream = File.OpenOutput(Starter.Provider.Sharedfolder, customPdfFilenameToSave, False)
                                             
               File.Copy2(Job.GetInputStream,OutStream)
               OutStream.Close

Just to give you some hints to start working with.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
it would not write the pdf
i guess it is written. But not to Starter.Provider.SharedFolder as you are using another path.
OpenPDF2(File.Combine(FindUserDownloadsFolder, customPdfFilenameToSave))
File.Combine returns a FILE Uri!

Change it to
B4X:
OpenPDF2(customPdfFilenameToSave)

For sure after you made the additional changes mentioned above.
 
Upvote 0

Mahimahi

Member
Licensed User
Thanks a lot DonManfred. Eliminating File.Combine solves my pdf opening issue. w.r.t. writing to Starter.Provider.SharedFolder, I revisited the project manifest and fixed up some issues and now it is working. Thanks.
 
Upvote 0
Top