Android Question httpJob downloaded pdf attempted to copy input stream to local storage failed

Status
Not open for further replies.

Mahimahi

Member
Licensed User
I attempted to download a pdf via httpJob by file copy http input stream to "OutStream". "FindUserDownloadsFolder" is File.DirInternal

OutStream = File.OpenOutput(FindUserDownloadsFolder, customPdfFilenameToSave, False)
File.Copy2(Job.GetInputStream,OutStream)

then I attempted to open it with OpenPDF
OpenPDF("file://" & File.Combine(FindUserDownloadsFolder, customPdfFilenameToSave))

Sub OpenPDF(FileName As String)
Dim i As Intent 'Requires a reference to the Phone library
i.Initialize(i.ACTION_VIEW, FileName)
i.SetType("application/pdf")
i.WrapAsIntentChooser("Choose PDF Viewer")
StartActivity(i)
End Sub

Error I got: android.os.FileUriExposedException: file:///data/user/0/com.abcd.doc.updater/files/abcd.pdf exposed beyond app through Intent.getData()

Where "com.abcd.doc.updater" is my Build Configurations, "Package Name". And I checked via internal storage of the phone, such path [internal storage]/android/data/com.abcd.doc.updater/ or any subdirectories of that do not exist.

What could be the issue here? Can someone shed some light? Is File.DirInternal the best place to store downloaded files? e.g. pdfs? Or a different place like File.DirDefaultExternal? Does access permission come into play for any place to store the downloaded file?
 

Mahimahi

Member
Licensed User
Thanks Erel. Here it is:

B4X:
OutStream = File.OpenOutput(FindUserDownloadsFolder, customPdfFilenameToSave, False)
File.Copy2(Job.GetInputStream,OutStream)

OutStream.Close

'then I attempted to open it with OpenPDF
OpenPDF("file://" & File.Combine(FindUserDownloadsFolder, customPdfFilenameToSave))

Sub OpenPDF(FileName As String)
   Dim i As Intent 'Requires a reference to the Phone library
   i.Initialize(i.ACTION_VIEW, FileName)
   i.SetType("application/pdf")
   i.WrapAsIntentChooser("Choose PDF Viewer")
   StartActivity(i)
End Sub

Sub FindUserDownloadsFolder As String
   Return File.DirInternal
End Sub
 
Last edited:
Upvote 0

Mahimahi

Member
Licensed User
Thanks DonManfred. I actually closed the outputstream. But neglected to include in the code above, I've updated the code now. Sorry about that. Is there something I am missing that caused the inability to save under [internal storage]/android/data/com.abcd.doc.updater/? Such path does not even exist when I checked with file manager. Is there access permission to File.DirInternal? Which path is best to save downloaded files like pdfs and to open with default pdf viewer? Please advise. Thanks.
 
Upvote 0

Mahimahi

Member
Licensed User
That's just amazing Erel. Thank you so much. It's like magic, it works now. A quick followup question regarding the "SetType", I had set it to "text/plain" despite I am opening a pdf and it works, but where can I lookup the different "SetType" I can set for other file types? other than the "image/*" in your example. Please advise. Thanks.
 
Upvote 0

Mahimahi

Member
Licensed User
Sorry posted the wrong code. I tried to store the input stream to Starter.Provider.Sharedfolder. It seems like storing of the file had failed, folder [internal storage]/android/data/com.abcd.doc.updater/ were not created under Android 8.0. On Android 6.0 it works great now, which before adding FileProvider was failing.

B4X:
OutStream = File.OpenOutput(FindUserDownloadsFolder, customPdfFilenameToSave, False)
File.Copy2(Job.GetInputStream,OutStream)

OutStream.Close

'then I attempted to open it with OpenPDF
OpenPDF("file://" & File.Combine(FindUserDownloadsFolder, customPdfFilenameToSave))

Sub OpenPDF(FileName As String)
   Dim i As Intent 'Requires a reference to the Phone library
   i.Initialize(i.ACTION_VIEW, FileName)
   Starter.Provider.SetFileUriAsIntentData(i, FileName)
   i.SetType("application/pdf")
   StartActivity(i)
End Sub

Sub FindUserDownloadsFolder As String
   Return Starter.Provider.SharedFolder
End Sub

Please advise. Thanks.
 
Upvote 0
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…