Android Tutorial Download list of images with HttpUtils2 and CustomListView

Discussion in 'Tutorials & Examples' started by Erel, Feb 6, 2013.

  1. Erel

    Erel Administrator Staff Member Licensed User

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


    [​IMG]

    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.
     

    Attached Files:

  2. sktanmoy

    sktanmoy Active Member Licensed User

    I was badly needed something like this one.
    Thanks a lot
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    Antonio1 likes this.
  4. Dogbonesix

    Dogbonesix Active Member Licensed User

    I like the download sample and I see where to get images on the WEB but what about images on my cell phone?
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    It is simpler to load local image files. Unless there are many images, you do not need to do it asynchronously.
     
  6. Douglas Farias

    Douglas Farias Expert Licensed User

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

    its possible call this on a design menu?
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    Very similar to the way you add it to an activity. You need to add it to a layout file and then call TabHost.AddTab.
     
  8. Mashiane

    Mashiane Expert Licensed 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...

    Code:
    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...

    Code:
    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?
     
    Kwame Twum, eSolution and Kahapdos like this.
  9. hillzx

    hillzx Member Licensed User

    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
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    Try to change the link to http instead of https.
     
  11. hillzx

    hillzx Member Licensed User

    once again you save my life, thanks
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice