Android Tutorial Flickr Viewer

Not open for further replies.

This application gets this html page: Flickr: Explore interesting photos from the last 7 days in FlickrLand... , parses it and finds the 9 image links.
It then sends a request for each of the images. Downloaded images are then displayed in the ImageViews.
Each time you press on the Connect button 9 new images appear.
Pressing on an image shows it larger in a second activity (note that the full image is not downloaded so the second activity just shows the thumbnail with its original size).

If you run this program you can see that all requests are handled in the background and that images appear whenever they are ready.

The TaskId plays an important role here. Each image request is sent with a TaskId between 0 to 8. This TaskId is later used to get the correct ImageView from the ImageViews array.

Sub Process_Globals
    Dim MainUrl As String
    MainUrl = ""
    Dim HttpClient1 As HttpClient
    Dim MainRequestId As Int
    MainRequestId = 100
End Sub

Sub Globals
    Dim btnConnect As Button
    Dim ImageView1 As ImageView
    Dim ImageView2 As ImageView
    Dim ImageView3 As ImageView
    Dim ImageView4 As ImageView
    Dim ImageView5 As ImageView
    Dim ImageView6 As ImageView
    Dim ImageView7 As ImageView
    Dim ImageView8 As ImageView
    Dim ImageView9 As ImageView
    Dim ivs() As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
    End If
    ivs = Array As ImageView(ImageView1, ImageView2, ImageView3, ImageView4, _
        ImageView5, ImageView6, ImageView7, ImageView8, ImageView9)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
Sub ResetImagesBackground
    For i = 0 To Activity.NumberOfViews - 1
        If Activity.GetView(i) Is ImageView Then Activity.GetView(i).Color = Colors.Black
    'We could have used the image views array (ivs) instead of going over all the views...
End Sub

Sub btnConnect_Click
    Dim request As HttpRequest
    HttpClient1.Execute(request, MainRequestId)
    ProgressDialogShow("Fetching data...")
End Sub

Sub Http_ResponseSuccess (Response As HttpResponse, TaskId As Int)
    Log(TaskId & ": success")
    If TaskId = MainRequestId Then 'Fetch the main page
        Response.GetAsynchronously("MainResponse", _ 
            File.OpenOutput(File.DirInternalCache, "page.html", False), True, MainRequestId)
    Else 'One of the images requests has arrived, write the image to a file in the background
        Response.GetAsynchronously("ImageResponse", _ 
            File.OpenOutput(File.DirInternalCache, "image" & TaskId, False), True, TaskId)
    End If
End Sub
Sub Http_ResponseError (Reason As String, StatusCode As Int, TaskId As Int)
    ToastMessageShow("Error. TaskId: " & TaskId & ", Reason: " & Reason & ", StatusCode: " & StatusCode, True)
End Sub
Sub ImageResponse_StreamFinish (Success As Boolean, TaskId As Int)
    If Success = False Then
        Msgbox(LastException.Message, "Error")
    End If
    Dim bd As BitmapDrawable 'load the image 
    bd.Initialize(LoadBitmap(File.DirInternalCache, "image" & TaskId)) 
    ivs(TaskId).Background = bd 'set the image to an ImageView
End Sub

Sub MainResponse_StreamFinish (Success As Boolean, TaskId As Int)
    HandleMainPage (File.OpenInput(File.DirInternalCache, "page.html"))
End Sub
'Parse the main page and find the 9 image links
Sub HandleMainPage (in As InputStream)
    start = DateTime.Now
    Dim TextReader1 As TextReader
    Dim pattern, class As String
    class = "<td class=" & QUOTE & "Photo" & QUOTE & ">"
    pattern = "img src=\q([^q]+)\q".Replace("q", QUOTE)
    Dim links As List
    Dim line As String
    line = TextReader1.ReadLine
    Do While line <> Null
        If line.IndexOf(class) > -1 Then
            Dim link As String
            Dim m As Matcher
            m = Regex.Matcher(pattern, line)
            If m.Find Then
                links.Add(m.Group(1)) 'add the image link
            End If
        End If
        line = TextReader1.ReadLine
    Log("done parsing main page: " & (DateTime.Now - start))
    For i = 0 To links.Size - 1 'send request for each image
        Dim request As HttpRequest
        HttpClient1.Execute(request, i)
End Sub

'Show the second activity with the chosen image.
Sub img_Click
    Dim iv As ImageView
    iv = Sender
    Dim bd As BitmapDrawable
    bd = iv.Background
    Activity2.Bitmap1 = bd.Bitmap
End Sub

A new version of FlickrViewer is available based on HttpUtils:
The new code is simpler and more robust.


    7.2 KB · Views: 1,747


Nice one!Do you perhaps have a example how to invoke the browser to display content of the webpage in your program?


thanks!so this will be more like a shortcut if im correct that opn the page in the browser?


Well-Known Member
Licensed User
Longtime User
Sorry for bumping that old post.
How can you dim imageviews as many as images are at a "site"? First to check how many are the images at site and then dim the same number of imageviews in order to load that images.(not only 9 imageviews)
Not open for further replies.