download image

ringmyr

Member
Licensed User
Longtime User
its only download the first image
whats is wrong?

B4X:
Sub Process_Globals
   Dim MainUrl As String
   MainUrl = "http://img222.imagevenue.com/gallery/loc683/336_1254_cars.php"
   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 = "morecars"
   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
 
Upvote 0

ringmyr

Member
Licensed User
Longtime User
hi have try the new one and i got this error:

B4X:
Starting Job: Main page
** Service (httputilsservice) Create **
** Service (httputilsservice) Start **
done parsing main page: 2
Starting Job: Images
** Service (httputilsservice) Start **
** Service (httputilsservice) Destroy **

and the app code is this

B4X:
Sub Process_Globals
   Dim MainUrl As String
   MainUrl = "http://img205.imagevenue.com/gallery/loc537/409_1999_cars.php"
End Sub

Sub Globals
   Dim ImageIndex As Int
   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)
   Activity.LoadLayout("1")
   ivs = Array As ImageView(ImageView1, ImageView2, ImageView3, ImageView4, _
      ImageView5, ImageView6, ImageView7, ImageView8, ImageView9)
   ResetImagesBackground
   HttpUtils.CallbackActivity = "Main"
   HttpUtils.CallbackJobDoneSub = "JobDone"
End Sub



Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub btnConnect_Click
   HttpUtils.CallbackUrlDoneSub = "" 'don't want to handle this event
   HttpUtils.Download("Main page", MainUrl)
   ProgressDialogShow("Fetching data...")
End Sub

Sub Activity_Resume
   'This is important as it is possible that the activity was in the background when the job has finished.
   If HttpUtils.Complete Then JobDone(HttpUtils.Job)
End Sub

'Usually we will want to have a single JobDone sub for all jobs.
'This allows us to check HttpUtils.Complete flag in Activity_Resume (as shown above).
Sub JobDone(Job As String)
   Select job
      Case "Main page"
         HandleMainPage
      Case "Images"
         ImagesJobDone
   End Select
   HttpUtils.Complete = False
End Sub
'Parse the main page and find the 9 image links
Sub HandleMainPage
   If HttpUtils.IsSuccess(MainUrl) = False Then
      ToastMessageShow("Error downloading main page.", True)
      Return
   End If
   ResetImagesBackground
   start = DateTime.Now
   Dim TextReader1 As TextReader
   TextReader1.Initialize(HttpUtils.GetInputStream(MainUrl))
   Dim pattern, class As String
   class = "cars"
   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))
   HttpUtils.CallbackUrlDoneSub = "ImageUrlDone"
   HttpUtils.DownloadList("Images", links)
   ImageIndex = 0
   btnConnect.Enabled = False
   ProgressDialogHide
End Sub

Sub ImageUrlDone (Url As String)
   ivs(ImageIndex).Bitmap = HttpUtils.GetBitmap(Url)
   ivs(ImageIndex).Gravity = Gravity.FILL
   ImageIndex = ImageIndex + 1
   'The SuccessfulUrls map holds all the successful links. We are removing this link from the list
   'to avoid dealing with this link again in ImagesJobDone
   HttpUtils.SuccessfulUrls.Remove(Url)
End Sub
Sub ImagesJobDone
   'In most cases this For loop will not be used, assuming that ImageUrlDone was called for each Url.
   'However if the activity was paused during the download it is possible that we have missed some calls.
   'So here we make sure that all downloaded images are visible.
   For i = 0 To HttpUtils.SuccessfulUrls.Size - 1
      ivs(ImageIndex + i).Bitmap = HttpUtils.GetBitmap(HttpUtils.SuccessfulUrls.GetKeyAt(i))
      ivs(ImageIndex + i).Gravity = Gravity.FILL
   Next
   btnConnect.Enabled = True
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
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.bmp = bd.Bitmap
   StartActivity(Activity2)
End Sub
 
Upvote 0

ringmyr

Member
Licensed User
Longtime User
ye no error in the log, but it stop after downloding 1 file:BangHead:

i have try to change pattern and class.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…