Android Question How to download and save a pdf file from google drive

Goles

Member
Licensed User
Hi everyone,
I want to download and save a pdf file from a google drive link in my app.
I use this code for this:
B4X:
Sub DownloadAndSaveFile(Link As String)
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(Link)
    ProgressDialogShow("Downloading...")
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        ProgressDialogHide
        Dim out As OutputStream = File.OpenOutput(File.DirInternal, "Tutorial.pdf", False)
        File.Copy2(j.GetInputStream, out)
       
        File.WriteString(File.DirInternal, "Tutorial.pdf", "Tutorial")
        Wait For (SaveAs(File.OpenInput(File.DirInternal, "Tutorial.pdf"), "application/octet-stream", "Tutorial.pdf")) Complete (Success As Boolean)
        Log("File saved successfully? " & Success)
       
        Log("Done!")
        out.Close
    Else
        Log(j.ErrorMessage)
    End If
    j.Release
End Sub

Sub SaveAs (Source As InputStream, MimeType As String, Title As String) As ResumableSub
    Dim intent As Intent
    intent.Initialize("android.intent.action.CREATE_DOCUMENT", "")
    intent.AddCategory("android.intent.category.OPENABLE")
    intent.PutExtra("android.intent.extra.TITLE", Title)
    intent.SetType(MimeType)
    StartActivityForResult(intent)
    Wait For ion_Event (MethodName As String, Args() As Object)
    If -1 = Args(0) Then 'resultCode = RESULT_OK
        Dim result As Intent = Args(1)
        Dim jo As JavaObject = result
        Dim ctxt As JavaObject
        Dim ContentResolver As JavaObject = ctxt.InitializeContext.RunMethodJO("getContentResolver", Null)
        Dim out As OutputStream = ContentResolver.RunMethod("openOutputStream", Array(jo.RunMethod("getData", Null), "wt")) 'wt = Write+Trim
        File.Copy2(Source, out)
        out.Close
        Return True
    End If
    Return False
End Sub

Sub StartActivityForResult(i As Intent)
    Dim jo As JavaObject = GetBA
    Dim ion As JavaObject
    ion = jo.CreateEvent("anywheresoftware.b4a.IOnActivityResult", "ion", Null)
    jo.RunMethod("startActivityForResult", Array(ion, i))
End Sub

Sub GetBA As Object
    Dim jo As JavaObject
    Dim cls As String = Me
    cls = cls.SubString("class ".Length)
    jo.InitializeStatic(cls)
    Return jo.GetField("processBA")
End Sub

It seems the file downloaded successfully, also I can see the downloaded file in correct direction (Downloads), But the file dose not open and the file size is very smaller than what it should be.
I think the file does not save correctly...
Any help with that?
 

Brian Dean

Well-Known Member
Licensed User
Longtime User
These lines of code retrieve the downloaded data and write it into a file named "Tutotial.pdf" in DirInternal . . .
B4X:
Dim out As OutputStream = File.OpenOutput(File.DirInternal, "Tutorial.pdf", False)
        File.Copy2(j.GetInputStream, out)

This code then overwrites that file with a file containing the text "Tutorial" . . .
B4X:
        File.WriteString(File.DirInternal, "Tutorial.pdf", "Tutorial")

Then this code saves the file containing "Tutorial" to a location chosen by the user . . .
B4X:
        Wait For (SaveAs(File.OpenInput(File.DirInternal, "Tutorial.pdf"), "application/octet-stream", "Tutorial.pdf")) Complete (Success As Boolean)

You need to remove the middle of these lines of code.
 
Upvote 0
Top