Android Question HttpUtils2 to Copy Multiple Files from Web to DirInternal

Marroq Spoulinker

Member
Licensed User
Longtime User
I've been reviewing the tutorials regarding HttpUtils2, and I'm struggling a little to understand something.

I can get an individual file downloaded and copied to DirInternal. But with this, I'm selecting an individual file and renaming it as I copy it to DirInternal.

What I need to do is to download many different files (currently dozens, but possibility of hundreds) and maintaining their file names (if it's named a.txt on the web location, I need it to go in to DirInternal and remain named a.txt).

I'm working from a list of file names, and can create multiple jobs to download each file. But in all the tutorials the steps to copy the files to DirInternal are in the JobDone sub and require a step to name the file. Since that's separate to where I'm initiating the download, I'm uncertain how to ensure the resulting file has the same name as the downloaded file.

I'm sure I'm just missing a small detail, but I'm really struggling.

Can anyone give me a hand with this?

Thank you!
 

DonManfred

Expert
Licensed User
Longtime User
Can you post a small project which shows your problem?
 
Upvote 0

Marroq Spoulinker

Member
Licensed User
Longtime User
My project that I'm actually working on it pretty big and contains some sensitive items. So I quickly mocked up a short example that is trying to download three files (a lot less than my real project) and tries to display them from the DirInternal folder. The app of course crashes with an error because the files don't exist (they do, but they are not named as expected).

Please see attached.

Thank you.
 

Attachments

  • failed-multi-file-download.zip
    391.3 KB · Views: 165
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Main")

    Dim job1, job2, job3 As HttpJob
    job1.Initialize("Job1",Me)
    Dim m As Map
    m.Initialize
    m.Put("filename","Visa.jpg")
    m.Put("imageview",ImageView1)
    job1.Tag = m
    job1.Download("http://amucu.org/wp-content/themes/amucu/images/universal/Visa.jpg")
   
    job2.Initialize("Job2",Me)
   
    Dim m As Map
    m.Initialize
    m.Put("filename","Mastercard.png")
    m.Put("imageview",ImageView2)
    job2.Tag = m
    job2.Download("http://amucu.org/wp-content/themes/amucu/images/universal/Mastercard.png")
   
    job3.Initialize("Job3",Me)
    Dim m As Map
    m.Initialize
    m.Put("filename","Discover.jpg")
    m.Put("imageview",ImageView3)
    job3.Tag = m
    job3.Download("http://amucu.org/wp-content/themes/amucu/images/universal/Discover.jpg")


    'You'll get an error when these lines of code are run, because the files don't exist.  They are downloaded,
    'But I can't get the Sub JobDone to correctly name them and move them to the DirInternal folder.
    '
    Dim bitmapholder As Bitmap
    If File.Exists(File.DirInternal,"Visa.jpg") Then
        bitmapholder.Initialize(File.DirInternal, "Visa.jpg")
    Else
        bitmapholder.Initialize(File.DirAssets, "placeHolder.jpg")
    End If
    ImageView1.Bitmap = bitmapholder
   
    Dim bitmapholder2 As Bitmap
    If File.Exists(File.DirInternal,"Mastercard.png") Then
        bitmapholder2.Initialize(File.DirInternal, "Mastercard.png")
    Else
        bitmapholder2.Initialize(File.DirAssets, "placeHolder.jpg")
    End If
    ImageView2.Bitmap = bitmapholder2
   
    Dim bitmapholder3 As Bitmap
    If File.Exists(File.DirInternal,"Discover.jpg") Then
        bitmapholder3.Initialize(File.DirInternal, "Discover.jpg")
    Else
        bitmapholder3.Initialize(File.DirAssets, "placeHolder.jpg")
    End If
    ImageView3.Bitmap = bitmapholder3

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub JobDone(job As HttpJob)
    If job.Success Then
        'This is the part I am really struggling with.  The examples in the tutorials all look like this:
        'Dim out As OutputStream = File.OpenOutput(File.DirInternal,"downloaded.txt", False)
        'But that is assigning a specific file name ("downloaded.txt") to the file, meaning all three files
        'that I am importing will get that name.
        '
        'I want the file name to remain what it was on the original file that was downloaded from the web.
        '(in this example, it would be Visa.jpg, Mastercard.png, and Discover.jpg)       
        '
        'I've tried changing "downloaded.txt" to be job.JobName, job.GetString, and a half-dozen other things,
        'but none of those options work.
        '
        Dim o As Object = job.Tag
        If o Is Map Then
            Dim m As Map = o
            Dim out As OutputStream = File.OpenOutput(File.DirInternal,m.Get("filename"), False) ' Use he TAG to write the file to disc with the right name. See declaration
            File.Copy2(job.GetInputStream, out)
            out.Close
       
            Dim bitmapholder As Bitmap
            If File.Exists(File.DirInternal,m.Get("filename")) Then
                bitmapholder.Initialize(File.DirInternal, m.Get("filename"))
                Dim img As ImageView = m.Get("imageview")
                img.Bitmap = bitmapholder
            End If
        End If       
       
    End If
    Dim l As List = File.ListFiles(File.DirInternal)
    Log(l)
    job.Release
End Sub
 

Attachments

  • FailedFixed.zip
    8.2 KB · Views: 159
Upvote 0

Marroq Spoulinker

Member
Licensed User
Longtime User
@DonManfred - Thank you, thank you, thank you. This has helped so much.

I guess I need to read up more on Tags and Maps, two things I haven't really utilized before, but were really the core of making this work. Ultimately, the Tag was the piece I was really lacking, some way to pass the file name from the initialization of the download to the JobDone Event.

I was able to take what I learned from your tweaking of my example and successfully integrate it in to my larger project.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
To just transport the filename the tag is perfect.
In my example i show you a bit more; using the map to transport more than just a filename. Transport the destination imageview (a reference) too

Remember: If you find my answer useful please click on like. Getting Likes is my honor.
 
Upvote 0
Top