So, I've used the below code in b4a, and I'm trying to get the same thing to happen in b4i. How can I download a pdf file from a website using the url, and open it with a pdf reader (of the users choice), and then if the user tries to download it again, and it's already been downloaded, it'll open the downloaded file. And all this happens on a button click.
I realise that there are ways of doing it by having the pdf in the app and stuff, but which this particular app, there are well over 100 pdf files to include, and it seems silly to force the user to download a ridiculously big app for the few files that the user might use.
So, here's the code, I hope someone can help. Thanks.
I realise that there are ways of doing it by having the pdf in the app and stuff, but which this particular app, there are well over 100 pdf files to include, and it seems silly to force the user to download a ridiculously big app for the few files that the user might use.
So, here's the code, I hope someone can help. Thanks.
B4X:
Sub Button2_Click
If File.exists(File.DirRootExternal, "D8xDeluxeInstallerGuide.pdf") Then
Dim intent1 As Intent
intent1.Initialize(intent1.ACTION_VIEW, "file://"&File.Combine(File.DirRootExternal, "D8xDeluxeInstallerGuide.pdf"))
intent1.SetType("application/pdf")
intent1.WrapAsIntentChooser("Choose PDF Viewer")
StartActivity(intent1)
Else
DOWNLOAD_ADDRESS = "http://nesscorporation.com/InstallationManual/Ness_D8xD16x_Deluxe_installer_manual.pdf"
DOWNLOAD_FILENAME = "D8xDeluxeInstallerGuide.pdf"
Dim DownloadManagerRequest1 As DownloadManagerRequest
DownloadManagerRequest1.Initialize(DOWNLOAD_ADDRESS)
DownloadManagerRequest1.Description="D8x Deluxe Installer Guide"
' save the download to external memory
' note you must manually update your project's manifest file adding android.permission.WRITE_EXTERNAL_STORAGE
DownloadManagerRequest1.DestinationUri="file://"&File.Combine(File.DirRootExternal, DOWNLOAD_FILENAME)
DownloadManagerRequest1.Title=DOWNLOAD_FILENAME
DownloadManagerRequest1.VisibleInDownloadsUi=True
DownloadId=DownloadManager1.Enqueue(DownloadManagerRequest1)
End If
End Sub
Sub DownloadManager1_DownloadComplete(DownloadId1 As Long)
' this does not guarantee that the download has actually successfully downloaded
' it means a DownloadMananger DownloadManagerRequest has completed
' we need to find that status of that request but only if that request matches the request we started
If DownloadId=DownloadId1 Then
' this is the download request we started
' query the DownloadManager for info on this request
Dim DownloadManagerQuery1 As DownloadManagerQuery
DownloadManagerQuery1.Initialize
DownloadManagerQuery1.SetFilterById(DownloadId)
' you must enable the SQL library to work with the Cursor object
Dim StatusCursor As Cursor
' pass our DownloadManagerQuery to the DownloadManager
StatusCursor=DownloadManager1.Query(DownloadManagerQuery1)
If StatusCursor.RowCount>0 Then
StatusCursor.Position=0
Dim StatusInt As Int
StatusInt=StatusCursor.getInt(DownloadManager1.COLUMN_STATUS)
Log("Download Status = "&Utils.GetStatusText(StatusInt))
If StatusInt=DownloadManager1.STATUS_FAILED Or StatusInt=DownloadManager1.STATUS_PAUSED Then
Dim ReasonInt As Int
ReasonInt=StatusCursor.GetInt(DownloadManager1.COLUMN_REASON)
Log("Status Reason = "&Utils.GetReasonText(ReasonInt))
End If
If StatusInt=DownloadManager1.STATUS_SUCCESSFUL Then
Dim intent1 As Intent
intent1.Initialize(intent1.ACTION_VIEW, "file://"&File.Combine(File.DirRootExternal, DOWNLOAD_FILENAME))
intent1.SetType("application/pdf")
intent1.WrapAsIntentChooser("Choose PDF Viewer")
StartActivity(intent1)
End If
Else
' always check that the Cursor returned from the DownloadManager Query method is not empty
Log("The DownloadManager has no trace of our request, it could have been cancelled by the user using the Android Downloads app or an unknown error has occurred.")
End If
StatusCursor.Close
StopService("")
End If
End Sub