Android Question Download File From Link

Juan Marrero

Active Member
Licensed User
Longtime User
Hi all. I'm having trouble downloading a file from a link (Google Drive and Dropbox shareable link) from my app. Code says Success but the file downloaded is corrupted and unusable.

This is with OKHttpUtils2
B4X:
j.Initialize("job", Me)
j.Download(modMain.shareableLinkGD) 'or modMain.shareableLinkDB

Sub JobDone(job As HttpJob)
   ProgressDialogHide
   
   If job.Success Then
       'we can check the of value job.JobName if there is more than one job
       'Activity.SetBackgroundImage(job.GetBitmap)
       Dim out As OutputStream = File.OpenOutput(File.DirDefaultExternal, "Songs_DB2.db3", False)
       File.Copy2(job.GetInputStream, out)
       out.Close
       ToastMessageShow("DB Downloaded Successfully", False)
   Else
       Log("Error: " & job.ErrorMessage)
   End If
   job.Release
End Sub

The file in Google Drive and Dropbox is 169Kb and the downloaded file ends up with 129Kb.
These are the urls for the file:
B4X:
Public shareableLinkGD As String = "https://drive.google.com/file/d/1E5er9sJJuuLJg2BCyGmBf91zlHl0srYZ/view?usp=sharing"
Public shareableLinkDB As String = "https://www.dropbox.com/s/9r224aj558j3ioa/Songs_DB.db3?dl=0"

I also used DownloadManager Library with the same results:
B4X:
dm.RegisterReceiver("dm")
           
           If Utils.IsInitialized=False Then
               Utils.Initialize(dm)
           End If
           
           Dim dmr As DownloadManagerRequest
           
           dmr.Initialize(modMain.shareableLinkGD)
           dmr.Description = "Download File " & "Songs_DB2.db3"
           dmr.DestinationUri = "file://" & File.Combine(File.DirDefaultExternal, "Songs_DB2.db3")
           dmr.Title = "Songs_DB2.db3"
           dmr.VisibleInDownloadsUi = True
   
           dID = dm.Enqueue(dmr)

Private Sub dm_DownloadComplete(DownloadId As Long)
   If dID = DownloadId Then
       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=dm.Query(DownloadManagerQuery1)
       If StatusCursor.RowCount > 0 Then
           StatusCursor.Position = 0
         
           Dim StatusInt As Int
           StatusInt = StatusCursor.getInt(dm.COLUMN_STATUS)
           Log("Download Status = " & Utils.GetStatusText(StatusInt))

           If StatusInt = dm.STATUS_FAILED Or StatusInt = dm.STATUS_PAUSED Then
               Dim ReasonInt As Int
               ReasonInt = StatusCursor.GetInt(dm.COLUMN_REASON)
               Log("Status Reason = " & Utils.GetReasonText(ReasonInt))
           End If
         
           If StatusInt = dm.STATUS_SUCCESSFUL Then
               'ImageView1.Bitmap=LoadBitmap(File.DirRootExternal, DOWNLOAD_FILENAME)
           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
     
       '   free system resources
       StatusCursor.Close
       dm.UnregisterReceiver
   End If
   
   ProgressDialogHide
End Sub

Any help is appreciated.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I've tested it in B4J with this code:
B4X:
Dim j As HttpJob
j.Initialize("", Me)
j.Download("https://drive.google.com/file/d/1E5er9sJJuuLJg2BCyGmBf91zlHl0srYZ/view?usp=sharing")
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
   Dim out As OutputStream = File.OpenOutput(File.DirApp, "1.dat", False)
   File.Copy2(j.GetInputStream, out)
   out.Close
   Log("success")
End If
j.Release
You can see that the file created is a html file. You are not downloading the correct resource. You are downloading the "download page".
 
Upvote 0

Juan Marrero

Active Member
Licensed User
Longtime User
Thanks for the clarification. Make sense. I tried some instructions on google on how to create the correct link string with no success. I ended up using the DropboxSync Library which is working.
 
Upvote 0

Juan Marrero

Active Member
Licensed User
Longtime User
Upvote 0
Top