Android Tutorial Download list of images with HttpUtils2 and CustomListView

Old and irrelevant tutorial. Many better examples, including this one: https://www.b4x.com/android/forum/threads/b4x-bitmapsasync.119589/


This example demonstrates an efficient way to download a list of images and show them in a CustomListView.


SS-2013-02-06_14.49.10.png


The first step is to download the "main" html page: Lists of Images on Popular Topics (Prints and Photographs Reading Room, Library of Congress)

The image links are then extracted and downloaded.

The CustomListView items are added after the page is parsed. The images are added when they are available.

The links and the images are stored in process global variables. This allows us to reuse these resources instead of downloading them again (after the activity is recreated).

All the downloading code is handled in a service named DownloadListService. It is easier to work with a service compared to an activity as the service life-cycle is much simpler and it doesn't get paused.

CallSubDelayed is used to update the activity whenever there is a new image available. Note that if the activity is currently paused (the user has pressed on the home button for example) then the activity will still be populated correctly.
 

Attachments

  • DownloadList.zip
    13.2 KB · Views: 5,976
Last edited:

Dogbonesix

Active Member
Licensed User
Longtime User
I like the download sample and I see where to get images on the WEB but what about images on my cell phone?
 

Douglas Farias

Expert
Licensed User
Longtime User
@Erel
how can i add this listview in a tabhost
tab1 tab2 tab3 ? *-*

its possible call this on a design menu?
 

Mashiane

Expert
Licensed User
Longtime User
This is very exciting for me. Thanks. I however wanted to get a list of files on a folder using this method, without knowing the file names first or the files extracted inside an html file. I would like to share...

Lets assume that you have a folder within your web server with image files and this folder is called "vehicles", you can then create a php script, lets name it "getVehicles.php" and upload it to our web server.

PHP:
<?php
If (isset($_REQUEST['file'])) {
  $file = basename($_REQUEST['file']);
  echo file_get_contents('./vehicles/'.$file);
} Else {
  If (is_dir('./vehicles') && $handle = opendir('./vehicles/')) {
    While (False !== ($entry = readdir($handle))) {
      If (!is_dir($entry)) {
        echo basename($entry)."\n";
      }
    }
    closedir($handle);
  } Else {
    header("HTTP/1.0 404 Not Found");
  }
}

Then your call to get a list of those files would be...

B4X:
Sub GetVehicles()
    Dim url As String = YOUR_URL & "/getVehicles.php"
    Dim Job1 As HttpJob  
    Job1.Initialize("VehiclesList", Me)
    Job1.PostString(url,"")
End Sub

The JobDone method then would be...

B4X:
Sub JobDone(Job As HttpJob)
If Job.Success = True Then
        Select Job.JobName
        Case "VehiclesList"
dim vList as string = Job.GetString
dim aList() as string = Regex.Split(CRLF, vList)
dim i as int
for i = 0 to aList.length - 1
log(aList(i))
next
end select
else
end if
job.release
end sub

The loop will return the name of each file name found within the directory. You can then reparse this file to return the file contents and display them or manipulate it!

Hope it helps someone?
 

hillzx

Member
Licensed User
Longtime User
All the downloading code is handled in a service named DownloadListService. It is easier to work with a service compared to an activity as the service life-cycle is much simpler and it doesn't get paused.

CallSubDelayed is used to update the activity whenever there is a new image available. Note that if the activity is currently paused (the user has pressed on the home button for example) then the activity will still be populated correctly.

I have followed your instruction in my project, i am trying to fetch data (text and images) from google news RSS, but it returns error, the image can not be downloaded in my android app, but works on chrome browser on my desktop, do you have a solution for this? please see my attachment
why.jpg
 
Top