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!!!
 

magarcan

Active Member
Licensed User
Longtime User
I recommend you to use HttpUtils. Especially if the image files are large.
Files are about one MB

I've been seeing httputils, and I know how to obtain the image, but I don't know how to save it...
:sign0104:
 
Upvote 0

magarcan

Active Member
Licensed User
Longtime User
You should use HttpUtils.GetInputStream with File.Copy2. There is an example here: http://www.b4x.com/forum/54676-post6.html
This is how I finally do:
B4X:
If HttpUtils.IsSuccess(imgurl) Then
       Dim b As Bitmap
       b = HttpUtils.GetBitmap(imgurl)
       Dim out As OutputStream
       out = File.OpenOutput(File.DirInternal, nimagen&".jpg", True)
       File.Copy2(HttpUtils.GetInputStream(imgurl), out)
       out.Close
      foto.Bitmap = LoadBitmap(File.DirInternal, nimagen&".jpg")
   End If
Do I need to use HttpUtils.GetInputStream istead of HttpUtils.GetBitmap???
 
Upvote 0

magarcan

Active Member
Licensed User
Longtime User
You are not doing anything with 'b' ?

Note that it is better to use LoadBitmapSample when working with large images.
Maybe, something like this?:
B4X:
   If HttpUtils.IsSuccess(imgurl) Then
       Dim out As OutputStream
       out = File.OpenOutput(File.DirInternal, nimagen&".jpg", True)
       File.Copy2(HttpUtils.GetBitmap(imgurl), out)
       out.Close
      foto.Bitmap = LoadBitmap(File.DirInternal, nimagen&".jpg")
   End If
 
Upvote 0

magarcan

Active Member
Licensed User
Longtime User
If you are only interested in the bitmap then you should not copy the file. Just call HttpUtils.GetBitmap.
What I only want to do is download and save the image file... How can I do this?
Thanks @Erel!!
 
Last edited:
Upvote 0

magarcan

Active Member
Licensed User
Longtime User
Here:
B4X:
If HttpUtils.IsSuccess(imgurl) Then
       Dim out As OutputStream
       out = File.OpenOutput(File.DirInternal, nimagen&".jpg", True)
       File.Copy2(HttpUtils.GetInputStream(imgurl), out)
       out.Close
      foto.Bitmap = LoadBitmap(File.DirInternal, nimagen&".jpg")
   End If
Thank you!!!
 
Upvote 0

ChrShe

Member
Licensed User
Longtime User
Please help with HttpUtils.GetBitmap...

If you are only interested in the bitmap then you should not copy the file. Just call HttpUtils.GetBitmap.

I keep getting "Object should first be initialized (bitmap)" from HttpUtils.GetBitmap.

Here's what I'm doing:

B4X:
Dim APhotoLink as string

APhotoLink = "http://www.petango.com/sms/photos/592/2bfc7af4-87b5-4725-8bc2-0aef83b5042b.jpg"

lvPets.AddTwoLinesAndBitmap(jName, otherInfo, [B]HttpUtils.GetBitmap(APhotoLink)[/B])

Any thoughts?
 
Upvote 0

pluton

Active Member
Licensed User
Longtime User
Try something like this. I didn't test it, just guessing

B4X:
Dim APhotoLink as string
Dim b As Bitmap

APhotoLink = "http://www.petango.com/sms/photos/592/2bfc7af4-87b5-4725-8bc2-0aef83b5042b.jpg"

b = HttpUtils.GetBitmap(APhotoLink)

lvPets.AddTwoLinesAndBitmap(jName, otherInfo, loadbitmap(b))


b = HttpUtils.GetBitmap(ImageUrl)
 
Upvote 0

ChrShe

Member
Licensed User
Longtime User
In retrospect, maybe I'm going about this the wrong way.

If you don't mind...Have a peek at the following link:

Adoptable Animals

I'm using HttpUtils.Download to download the page, then parsing through it to get the info that I want.
Considering that the Animal Photos are downloaded with the page in HttpUtils.Download, shouldn't there be a way that I can get the photos from memory...or are they loaded in a temp folder somewhere?

???

Thanks for the help!!!!!!!
Chris
 
Upvote 0

ChrShe

Member
Licensed User
Longtime User
Thanks a TON!

I did look at the Flicker example...but will dig into it more in-depth.

...Will report back with findings. :)

~Chris
 
Upvote 0

clx

Member
Licensed User
Longtime User
ChrShe, here is a solution.. be sure to include the HttpUtils modules in your project and add HTTP lib.

hope you can find this useful.

B4X:
Sub Process_Globals
   Dim ImageUrl As String
   ImageUrl = "http://www.petango.com/sms/photos/592/d485b787-aaa6-4d66-9fb6-4c6e17c8022e_TN1.jpg"
End Sub

Sub Globals
   Dim ListView1 As ListView
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("main")
   HttpUtils.CallbackActivity = "Main"
   HttpUtils.CallbackJobDoneSub = "JobDone"
   HttpUtils.CallbackUrlDoneSub = "UrlDone"
   HttpUtils.Download("GET Job1", ImageUrl) 'start 
End Sub

Sub Activity_Resume
   If HttpUtils.Complete = True Then JobDone(HttpUtils.Job)
End Sub

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

Sub JobDone (Job As String)
   Select Job
      Case "GET Job1"
         If HttpUtils.IsSuccess(ImageUrl) Then 
            Dim b As Bitmap
            b = HttpUtils.GetBitmap(ImageUrl)
            ListView1.AddTwoLinesAndBitmap2("test", "test", b, "link")
         End If
   End Select
   HttpUtils.Complete = False
End Sub
 
Upvote 0

ChrShe

Member
Licensed User
Longtime User
1st off...sorry to be a pain. :sign0144:

Thanks! That gets me close!

So, what I'm doing is downloading an html page and parsing out what I want...creating ListViewItems built on what gets parsed out.
One of the bits that I'm parsing out is a link to a photo. Then doing a ListView.AddTwoLinesAndBitmap.

What I'm seeing by calling HttpUtils.Download and passing in the URL to the photo, is that the part where it tries to add the listitem happens before HttpUtils returns the photo, and since I'm trying to add an item with a photo that isn't downloaded yet, the add fails and I end up with 0 listitems. :s

Lemme go ahead and post my code here, in all of it's ugliness...perhaps that'll help:

B4X:
Sub Globals
   Type ListViewData (FirstRow As String, SecondRow As String, ANum As String, Photo As Bitmap)
   '...
   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"
   '...
        HttpUtils.Download("GetPageFromPetango", Main.AnimalURL)
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 ParsePetangoPage
       'declare some variables
       '...

   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
End Sub
 
Upvote 0

pluton

Active Member
Licensed User
Longtime User
Did you try what you get on abmpurl = line
What is Main.AnimalURL ???

You must post all code. Export it. It is hard to help you with half of the code

B4X:
abmpurl = line
 
Last edited:
Upvote 0
Top