Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4android > Basic4android Getting started & Tutorials
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Basic4android Getting started & Tutorials Android development starts here. Please do not post questions in this sub-forum.

Flickr Viewer

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-21-2010, 02:07 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default Flickr Viewer



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.

Code:
Sub Process_Globals
    
Dim MainUrl As String
    MainUrl = 
"http://www.flickr.com/explore/interesting/7days/"
    
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
        HttpClient1.Initialize(
"http")
    
End If
    Activity.LoadLayout(
"1")
    ivs = 
Array As ImageView(ImageView1, ImageView2, ImageView3, ImageView4, _
        ImageView5, ImageView6, ImageView7, ImageView8, ImageView9)
    ResetImagesBackground
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
    
Next
    
'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
    request.InitializeGet(MainUrl)
    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)
    
ProgressDialogHide
End Sub
'
Sub ImageResponse_StreamFinish (Success As Boolean, TaskId As Int)
    
If Success = False Then
        
Msgbox(LastException.Message, "Error")
        
Return
    
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)
    ResetImagesBackground
    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
    TextReader1.Initialize(in)
    
Dim pattern, class As String
    class = 
"<td class=" & QUOTE & "Photo" & QUOTE & ">"
    pattern = 
"img src=\q([^q]+)\q".Replace("q"QUOTE)
    
Dim links As List
    links.Initialize
    
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
    
Loop
    TextReader1.Close
    
Log("done parsing main page: " & (DateTime.Now - start))
    
For i = 0 To links.Size - 1 'send request for each image
        Dim request As HttpRequest
        request.InitializeGet(links.Get(i))
        HttpClient1.Execute(request, i)
    
Next
    
ProgressDialogHide
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
    
StartActivity(Activity2)
End Sub
A new version of FlickrViewer is available based on HttpUtils: http://www.basic4ppc.com/forum/basic...html#post50919
The new code is simpler and more robust.
Attached Files
File Type: zip FlickrViewer.zip (7.2 KB, 479 views)
Reply With Quote
  #2 (permalink)  
Old 11-25-2010, 11:24 AM
Junior Member
 
Join Date: Nov 2010
Posts: 10
Default

Nice one!Do you perhaps have a example how to invoke the browser to display content of the webpage in your program?
Thanks
Reply With Quote
  #3 (permalink)  
Old 11-25-2010, 02:42 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

You can open the browser with the Phone library:
http://www.basic4ppc.com/forum/basic...-web-page.html
Reply With Quote
  #4 (permalink)  
Old 12-03-2010, 09:13 AM
Junior Member
 
Join Date: Nov 2010
Posts: 10
Default

thanks!so this will be more like a shortcut if im correct that opn the page in the browser?
Reply With Quote
  #5 (permalink)  
Old 12-03-2010, 09:13 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

Yes.
Reply With Quote
  #6 (permalink)  
Old 11-06-2011, 08:59 PM
vb1992's Avatar
Basic4ppc Expert
 
Join Date: Oct 2011
Location: USA
Posts: 594
Default

How could we show all the photos stored in our phone like this?
Reply With Quote
  #7 (permalink)  
Old 11-07-2011, 06:50 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

You should start this question in a new thread.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Embedded PDF Viewer in Form IansResearch Questions (Windows Mobile) 1 03-15-2010 07:24 PM
Map/Viewer for open street map schimanski Questions (Windows Mobile) 3 10-21-2009 11:43 AM
Ozi Files Viewer BerndB Share Your Creations 1 01-08-2009 07:43 PM
SQLite Viewer Erel Open Source Projects 5 11-15-2008 07:12 AM
Text/Pic Viewer scott93727 Share Your Creations 0 05-02-2007 05:28 AM


All times are GMT. The time now is 10:18 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0