Download image

magarcan

Active Member
Licensed User
Longtime User
Which is the easiest way to download an image?? What I want to do, is check if an image exist, if it doen't, donwload it, and then, load it.

This is my code:
B4X:
If File.Exists(File.DirDefaultExternal, image&n".jpg") Then
      foto.Bitmap = LoadBitmap(File.DirDefaultExternal, image&n".jpg")
   Else
      'I've to get and save the image, then load it
      
      foto.Bitmap = LoadBitmap(File.DirDefaultExternal, image&n".jpg")
   End If
Thanks!!!
 

ChrShe

Member
Licensed User
Longtime User

Here's the activity code...
Sorry it's so ugly...I haven't cleaned it up yet:

Main.AnimalURL is passed in. An example of it's value is:

http://www.petango.com/webservices/...om/WebServices/adoptablesearch/css/styles.css

It points to:
Adoptable Animals

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

   'Create a ListViewData object 
   Type ListViewData (FirstRow As String, SecondRow As String, ANum As String, Photo As Bitmap)
   
   Dim lvPets As ListView
   Dim pnlPets As Panel
   
   Dim abmpurl As String
   Dim abmp As Bitmap
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
   HttpUtils.CallbackActivity = "actPets" 'Current activity name.
    HttpUtils.CallbackJobDoneSub = "JobDone"'"GetPageFromPetango"
   HttpUtils.CallbackUrlDoneSub = "URLDone"
   
   lvPets.Initialize("View")
   Activity.LoadLayout("actPets.bal")
   
   pnlPets.Visible = True
   ProgressDialogShow("Loading please wait...")
   HttpUtils.Download("GetPageFromPetango", Main.AnimalURL)
   
   pnlBottom = 0
   svAnimals.Color = Colors.Black
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub lvPets_ItemClick (Position As Int, Value As Object)
   Dim lvd As ListViewData
   lvd = value
   Msgbox(lvd.ANum, "AHS")
End Sub

Sub UrlDone(Url As String)
   Log(Url & " done")
End Sub

Sub JobDone (Job As String)
   Select Job
      Case "GetPageFromPetango"
         ParsePetangoPage
      Case "GetAnimalPhoto"
         If HttpUtils.IsSuccess(abmpurl) Then 
            abmp = HttpUtils.GetBitmap(abmpurl)
         End If
   End Select
   HttpUtils.Complete = False
End Sub

Sub GetPageFromPetango(pg As String)
   Dim s As String
   Do Until HttpUtils.IsSuccess(pg) = False
      Log("Still Waiting...")
      DoEvents
   Loop
   If HttpUtils.IsSuccess(pg) Then
      s = HttpUtils.GetString(pg)
   End If
   ParsePetangoPage'(s)
End Sub
Sub ParsePetangoPage
' The below is a table row, from the returned page that contains animal information
'
'   <tr>
'   <td class="list-item">
'   <div class="list-animal-photo-block">
'   <a href="wsAdoptableAnimalDetails.aspx?id=13381783&css=http://petango.com/WebServices/adoptablesearch/css/styles.css" ><img class="list-animal-photo" src="http://www.petango.com/sms/photos/592/dfb2d260-2629-4cca-a67c-646812063375_TN1.jpg" alt="Photo"></a>
'   </div>
'   <div class="list-animal-info-block">
'   <div class="list-animal-name"><a href="wsAdoptableAnimalDetails.aspx?id=13381783&css=http://petango.com/WebServices/adoptablesearch/css/styles.css" >Cardamon</a></div>
'   <div class="list-animal-id">13381783</div>
'   <div class="list-anima-species">Cat</div>
'   <div class="list-animal-sexSN">Female/Spayed</div>
'   <div class="list-animal-breed">Domestic Shorthair/Mix</div>
'   <div class="list-animal-age">2 years 4 months</div>
'   <div class="hidden">Cat Condo</div>
'   </div>
'   </td>

   Dim link As String
   Dim APhoto As ImageView
   Dim wvAnimalPhotoTh As WebView
   Dim AName As String
   Dim ANumber As String
   Dim ADesc As String
   Dim ASex As String
   Dim AAge As String
   Dim ABreed As String
   
   Dim jName As String
   Dim jNumber As String
   Dim jSex As String
   Dim jAge As String
   Dim jBreed As String
   
   'Dim APhoto As Bitmap
   Dim APhotoLink As String

   'Create a TestReader to parse the file
   Dim txtrdr As TextReader
   txtrdr.Initialize(HttpUtils.GetInputStream(Main.AnimalURL))
   
   Dim line As String
   line = txtrdr.ReadLine
   Do While line <> Null
      Dim lvd As ListViewData
      lvd.Initialize
      Dim b As Bitmap
      'Animal Photo
      If line.Contains("img class=") Then
         line = line.SubString(line.IndexOf("w"))
         line = "<a href=" & QUOTE & "http://www.petango.com/webservices/adoptablesearch/" & line
         line = line.SubString2(line.IndexOf("src="), line.IndexOf("alt="))
         line = line.SubString(line.IndexOf(QUOTE & "h"))
         line = line.SubString2(line.IndexOf("http"),line.Length-1)
         line = line.Replace(QUOTE, "")
         abmpurl = line
         HttpUtils.Download("GetAnimalPhoto", abmpurl)
      End If
      'Animal Name
      If line.Contains("list-animal-name") Then
         line = line.SubString(line.IndexOf("w"))
         line = "<a href=" & QUOTE & "http://www.petango.com/webservices/adoptablesearch/" & line
         AName = line
         jName = AName.SubString2(AName.IndexOf(">")+1, AName.IndexOf("</"))
      End If
      'Animal ID
      If line.Contains("list-animal-id") Then
         ANumber = line
         ANumber = ANumber.SubString2(ANumber.IndexOf(">")+1, ANumber.IndexOf("</"))
      End If
      'Animal Sex
      If line.Contains("list-animal-sexSN") Then
         ASex = line
         ASex = ASex.SubString2(ASex.IndexOf(">")+1, ASex.IndexOf("</"))
      End If
      'Animal Age
      If line.Contains("list-animal-age") Then
         AAge = line
         AAge = AAge.SubString2(AAge.IndexOf(">")+1, AAge.IndexOf("</"))
      End If
      'Animal Breed
      If line.Contains("list-animal-breed") Then
         ABreed = line
         ABreed = ABreed.SubString2(ABreed.IndexOf(">")+1, ABreed.IndexOf("</"))
      End If
      'End of this animal's table row
      If line = "</td>" Then
         lvd.FirstRow = jName
         lvd.SecondRow = AAge & " " & ASex
         lvd.ANum = ANumber
         lvd.Photo = abmp
         lvPets.AddTwoLinesAndBitmap2(lvd.FirstRow, lvd.SecondRow, lvd.Photo, lvd)
      Else
         'Don't do anything
      End If
      line = txtrdr.ReadLine
   Loop
   txtrdr.Close
   ProgressDialogHide
End Sub

Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
   Select Case KeyCode
      Case KeyCodes.KEYCODE_BACK
         Main.AnimalURL = ""
   End Select
End Sub
 
Last edited:
Upvote 0

ChrShe

Member
Licensed User
Longtime User
Okay folks...

Thanks much for all the help. I ended up using a scrollview and using webview.loadurl to get the animal photos.

But I don't understand why HttpUtils.GetBitmap wouldn't work?

If anyone can explain that I would greatly appreciate it.

Again...thanks MUCH, and Happy Holidays!!!!

Chris
 
Upvote 0

ChrShe

Member
Licensed User
Longtime User
Ah...so, GetInputStream is still running when I try to call GetBitmap...?

That helps immensely!
Thanks a ton!!!

Oh...I haven't mentioned this, but having looked at the HttpClient stuff, HttpUtils is a vast improvement! Thanks for putting it together!

~Chris
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…