httputils2 (preferably) to upload images

Malky

Active Member
Licensed User
Longtime User
Hi, I have been going through most possibilities here and need to download small image files from my php server. I think I am confusing myself now as I have tried too many possible methods/examples with no success.

For my app, I want to replicate my small SQL Lite database on the external(?) card and download the actual files onto the phone. (Image filenames stored in the DB).

Preferably I would like to download them all or sections of them in a list after inserting each table with image names.

When I have tried this I get the last file downloaded to the phone valid, but the previous ones are invalid. Is there an example of downloading a single image, or list of images to the external card?

Most just display the image in an imageview after download, but not copying to external and the flickr one means ripping a page apart which I don't need.

Thanks in advance,

Malky
 
Last edited:

Malky

Active Member
Licensed User
Longtime User
Hi Erel, unfortunately I am at work at the moment and possibly changed the code ayway trying different things.

Basically I want to get an image filename from my db, and download that image from my server to the external phone card.
I thought I could do this with the file.copy option, but I don;t know where the file is after it is download to use that function.

As I will be downloading quite a few small files, it could make more sense to use the flickr functionality if I knew where the files were to file.copy them?

Hope that makes sense? Even if you gave me a line number where to put it?

I'll see what I have left tonight after cannabilising my code once again.

Thanks for the reply,

Malky
 
Upvote 0

Malky

Active Member
Licensed User
Longtime User
Hi Erel, thanks for the tip, but I still seem to be ending up with the last image only?

B4X:
'Activity module
Sub Process_Globals
   Dim SD_PATH As String
   Dim imagename As String
   SD_PATH = File.DirRootExternal & "/homerbg2/data/"
   Dim URL As String
   URL = "http://www.homersparadise.com/"
End Sub

Sub Globals
   Dim bmp As Bitmap
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Dim job1 As HttpJob
   job1.Initialize("Job1", Me)
   imagename = "en.png"
   job1.Download(URL & "images/prop_apps/thumbs/langs/" & imagename)
      imagename = "bg.png"
   job1.Download(URL & "images/prop_apps/thumbs/langs/" & imagename)

End Sub

Sub JobDone (Job As HttpJob)
   Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
   If Job.Success = True Then
      Dim out As OutputStream
      out = File.OpenOutput(SD_PATH, imagename, False)
      File.Copy2(Job.GetInputStream,out)
      out.Close
   Else
      Log("Error: " & Job.ErrorMessage)
      ToastMessageShow(SD_PATH & imagename, True)
   End If
   Job.Release
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

I'm missing something as I got part of an image? Originally I'd forgotten the out.close but I now get the full image, but only the last one?

I tried two just hardcoded to see if it works, but always get the last image only on my phone.

Malky
 
Upvote 0

Malky

Active Member
Licensed User
Longtime User
Hi Erel, thanks for the help, but I cannot determine how many jobs I will need until after I have made the queries to the db?

The first time it will be 4 as I know I have 4 languages in there, but other queries may return 10 areas and the following query could return 40-50 villages, all of which would require the relevant image for the listview or Scrollview.

Even with the Flickr example, there is a pre-determined amount of images to download. I don't have that luxury?

Cheers,

Malk

Malky
 
Upvote 0

Malky

Active Member
Licensed User
Longtime User
Hi again Erel, sorry to be a pain in the proverbial, but I am still only getting the last image saved to the sd card for some reason?

I though the out command would close each file when done?
B4X:
'Activity module
Sub Process_Globals
   Dim SD_PATH As String
   Dim imagename As String
   SD_PATH = File.DirRootExternal & "/homerbg2/data/"
   Dim URL As String
   URL = "http://www.homersparadise.com/"
   Dim testList As List

End Sub

Sub Globals
   Dim bmp As Bitmap
End Sub

Sub Activity_Create(FirstTime As Boolean)
   testList.Initialize
   Dim arr(2) As String
   arr(0)="en.png"
   arr(1)="bg.png"
   testList.Initialize
   testList.Add(arr)
   testList.AddAll(testList)
   For i = 0 To 1
      Dim job As HttpJob
      job.Initialize("Job", Me)
      imagename = arr(i)
      job.Download(URL & "images/prop_apps/thumbs/langs/" & imagename)
   Next

End Sub

Sub JobDone (Job As HttpJob)
   Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
   If Job.Success = True Then
      Dim out As OutputStream
      out = File.OpenOutput(SD_PATH, imagename, False)
      File.Copy2(Job.GetInputStream,out)
      out.Close
   Else
      Log("Error: " & Job.ErrorMessage)
      ToastMessageShow(SD_PATH & imagename, True)
   End If
   Job.Release
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
 
Upvote 0

Malky

Active Member
Licensed User
Longtime User
Just back from work and tried changing a few things, but still only the last file gets copied to the phone?

The log error is always false after calling the job.download function?
B4X:
'Activity module
Sub Process_Globals
   Dim SD_PATH As String
   Dim imagename As String
   SD_PATH = File.DirRootExternal & "/homerbg2/data/"
   Dim URL As String
   URL = "http://www.homersparadise.com/"
   Dim testList As List

End Sub

Sub Globals
   Dim bmp As Bitmap
End Sub

Sub Activity_Create(FirstTime As Boolean)
   testList.Initialize
   Dim pics(2) As String
   pics(0)="en.png"
   pics(1)="bg.png"
   
   testList.add(pics)
   'testList.AddAll(testList)
   testList.Initialize
   For i = 0 To 1
      Dim job As HttpJob
      imagename = pics(i)
      job.Initialize("Job" & i, Me)
      
      job.Download(URL & "images/prop_apps/thumbs/langs/" & imagename)
      Log(URL & "images/prop_apps/thumbs/langs/" & imagename & " - " & job.Success)
   Next

End Sub

Sub JobDone (Job As HttpJob)
   Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
   If Job.Success = True Then
      Dim out As OutputStream
      out = File.OpenOutput(SD_PATH, imagename, False)
      File.Copy2(Job.GetInputStream,out)
      out.Close
   Else
      Log("Error: " & Job.ErrorMessage)
      ToastMessageShow(SD_PATH & imagename, True)
   End If
   Job.Release
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

LOG
Installing file.
PackageAdded: package:anywheresoftware.b4a.samples.httputils2
** Activity (main) Create, isFirst = true **
http://www.homersparadise.com/images/prop_apps/thumbs/langs/en.png - false
http://www.homersparadise.com/images/prop_apps/thumbs/langs/bg.png - false
** Activity (main) Resume **
** Service (httputils2service) Create **
** Service (httputils2service) Start **
** Service (httputils2service) Start **
JobName = Job1, Success = true
JobName = Job0, Success = true
** Service (httputils2service) Destroy **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
 
Upvote 0

Malky

Active Member
Licensed User
Longtime User
UPDATE!!
I have found where my error was and it was in the imagename variable in the JobDone function. This was never changing due to my coding, therefore only the first imagename offered was used throughout the loop.

DOH!

Apologies for wasting anyone's time looking at this,

Malky
 
Last edited:
Upvote 0

Malky

Active Member
Licensed User
Longtime User
I think my problem has only moved on?

I still cannot populate the listview correctly as the images are still not completely downloaded.

Is there any way to 'stall' the app until these are complete before continuing?

Thanks,

Malky
 
Upvote 0

Malky

Active Member
Licensed User
Longtime User
Hi Erel, How do I stop the next part of the code running until then though?

In my log, the jobs are done, but this seems to happen after the population of the listview (or during it).

I do need to start another download for my language diction table and to store that in the DB, so I may try that tonight and hope that by the time that has completed, I can then safely start populating the listview as the files should be complete by then?

Cheers,

Malky
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
There are several ways to implement it. You can count the number of completed jobs and then start filling the list from JobDone when all jobs completed.

A more elegant approach is to add the jobs to a list and then remove each completed job from the list.
When the list is empty you should populate the ListView.
B4X:
Sub JobDone (Job As HttpJob)
 ListOfJobs.RemoveAt(ListOfJobs.IndexOf(Job))
 If Job.Success Then

 ...
End If
If ListOfJobs.Size = 0 Then
  FillListView
End If
End Sub

Another option is to use CustomListView class instead of ListView. This will allow you to add the images after the items were added.
 
Upvote 0

Malky

Active Member
Licensed User
Longtime User
Hey Erel, this sounds fanatastic, thanks very much for that. Looks like what I need.

Can't wait to get home now :)

I was under the (false) impression that the listview only required the path and filenames to the images and fetched them when it was opened, so this explains a lot.

Enjoy your day and thanks again,

Malky
 
Upvote 0

Malky

Active Member
Licensed User
Longtime User
Got it at last.

Thanks very much Erel, I can go to bed now and sleep happily.

Great work, can't thank you guys enough.

Cheers,

Malky
 
Last edited:
Upvote 0
Top