Android Question Download PDF

kozbot

Member
Licensed User
Longtime User
Hi guys,

I've been trying to adapt Erel's tutorial and this one as well to match what I would like, but I'm failing miserably... I'm trying to download a pdf on a button click to the phone, for the user to open in another app of their choosing, when they'd like. Some of the pdf's that need to be downloaded will be rather large. I can't seem to understand the right way to go about this. The link I'm downloading from is here.

If anyone can help me out I'd be so appreciative!

Thanks.
 

kozbot

Member
Licensed User
Longtime User
Hi Erel,

Yeah I did try to mod that for my liking, but I think I'm just not doing it right. I'll try again, and see how I go.

Thanks for the help so far!
 
Upvote 0

kozbot

Member
Licensed User
Longtime User
Can you please tell me if I even have this in the right place? or if I'm missing anything?

B4X:
ub Process_Globals
End Sub

Sub Globals
    Dim Button2 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("InstallerUserQuick")
    Activity.Title = "D8"
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    StartActivity("D8Quick")
End Sub
Sub Button2_Click
    Dim job As HttpJob
    job.Initialize("j", Me)
    job.Download("http://www.k2mdev.com.au/ness/PDF/D8_installer_manual_rev4.7.pdf")
End Sub
Sub JobDone(job As HttpJob)
  If job.Success Then
    Dim out As OutputStream = File.OpenOutput(File.DirRootExternal, "D8Installerguide.pdf", False)
    File.Copy2(job.GetInputStream, out)
    out.Close '<------ very important
  Else
    Log("Error: " & job.ErrorMessage)
  End If
  job.Release
End Sub

Thanks
 
Upvote 0

kozbot

Member
Licensed User
Longtime User
I wonder why it's not working then... It doesn't download the file at all, it just shows a flash of the icon of the file and that's it. I'm really lost.

I've for the HttpUtils2 library ticked, it's version 2.01. Do I need another library as well?

Again, thank you for your help so far
 
Upvote 0

kozbot

Member
Licensed User
Longtime User
That's just it, it doesn't download at all for me... I should be downloading a pdf file, but nothing happens for me... did that work for you?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Works fine here:
SS-2014-07-24_09.23.15.png
 
Upvote 0

kozbot

Member
Licensed User
Longtime User
It doesn't do anything when I click the button. You know how when you're downloading a file from a site, you get the arrow in the corner saying that it's downloading? I'm not getting that at all, it doesn't seem to do anything, or do I need to add something so that the arrow shows up?

EDIT: I just had a look, and my phone isn't downloading anything at all when I press the button.

I also received the log message:
Service ServiceRecord{448699a0 u0 com.estrongs.android.pop/com.estrongs.android.ui.notification.ESTaskService} in process ProcessRecord{445e5810 14624:com.estrongs.android.pop/u0a77} not same as in map: null
 
Last edited:
Upvote 0

kozbot

Member
Licensed User
Longtime User
Is there a way of downloading it with the internal downloading service so that it's obvious that it's downloading? I am at my wits end...

I tried it again with this, I think this is the breakpoint you were talking about:

B4X:
Sub Button2_Click
    Dim job As HttpJob
    job.Initialize("j", Me)
    job.Download("http://www.k2mdev.com.au/ness/PDF/D8_installer_manual_rev4.7.pdf")
End Sub
Sub JobDone(job As HttpJob)
  If job.Success Then
    Dim out As OutputStream = File.OpenOutput(File.DirRootExternal, "D8Installerguide.pdf", False)
    File.Copy2(job.GetInputStream, out)
    out.Close
  Else
    Log("Error: " & job.ErrorMessage)
  End If
  job.Release
End Sub

But again, nothing happened. I know that if I take JobDone out, it doesn't work at all, but I can't figure out what's missing to get it to download using the internal downloading service so that it's available to be opened using the notifications.
 
Upvote 0

kozbot

Member
Licensed User
Longtime User
OK! I'm almost there... the last part is the problem when opening it with adobe I get an error: "This Document Could Not Be Opened" BUT when I go to Adobe and look at my files it's there and opens perfectly. Any ideas?

This is my code now, and I must admit, I don't understand half of it, so just to ask, do I need to add or remove anything?

Thanks again

B4X:
Sub Process_Globals
    Private ph As Phone
    Dim DownloadId As Long
End Sub

Sub Globals
    Dim DOWNLOAD_ADDRESS As String="http://www.k2mdev.com.au/ness/PDF/D8_installer_manual_rev4.7.pdf"
    Dim DOWNLOAD_FILENAME As String="D8InstallerGuide.pdf"
    Dim Button2 As Button
    Dim DownloadManager1 As DownloadManager
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("InstallerUserQuick")
    Activity.Title = "D8"
    DownloadManager1.RegisterReceiver("DownloadManager1")
    If Utils.IsInitialized=False Then
      Utils.Initialize(DownloadManager1)
      End If
End Sub

Sub Activity_Resume
    ph.SetScreenOrientation(1)
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button2_Click
Dim DownloadManagerRequest1 As DownloadManagerRequest
      DownloadManagerRequest1.Initialize(DOWNLOAD_ADDRESS)
      DownloadManagerRequest1.Description="D8_installer Demo"
      DownloadManagerRequest1.DestinationUri="file://"&File.Combine(File.DirRootExternal, DOWNLOAD_FILENAME)
      DownloadManagerRequest1.Title=DOWNLOAD_FILENAME
      DownloadManagerRequest1.VisibleInDownloadsUi=True
 
  DownloadId=DownloadManager1.Enqueue(DownloadManagerRequest1)
End Sub
Sub DownloadManager1_DownloadComplete(DownloadId1 As Long)
  If DownloadId=DownloadId1 Then
      Dim DownloadManagerQuery1 As DownloadManagerQuery
      DownloadManagerQuery1.Initialize
      DownloadManagerQuery1.SetFilterById(DownloadId)
     
      Dim StatusCursor As Cursor
      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, DOWNLOAD_FILENAME)
            intent1.SetType("application/pdf")
            intent1.WrapAsIntentChooser("Choose PDF Viewer")
            StartActivity(intent1)
        End If
       
      Else
        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
 
Upvote 0
Top