Display an Image from a server

ciginfo

Well-Known Member
Licensed User
Longtime User
Hello,
How to load and display an image in an ImageView from a server, the simplest.
I finf different ways in various demos, FlickrViewver, FlickrViewver2, HttpUtilsExample. etc and I'm lost (different libraries etc..).
I'm looking for the easiest way to display this "http://ciginfo.alwaysdata.net/img_sites/Img1.jpg" image in a ImageView.
thank you
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Add a reference to HttpUtils2 library. Create a layout file with ImageView1:
This is the simplest code:
B4X:
Sub Globals
   Dim ImageView1 As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("1")
   Dim job As HttpJob
   job.Initialize("image", Me)
   job.Download("http://ciginfo.alwaysdata.net/img_sites/Img1.jpg")
End Sub

Sub JobDone(Job As HttpJob)
   If Job.Success Then
      ImageView1.SetBackgroundImage(Job.GetBitmap)
   End If
   Job.Release
End Sub

However it is better to cache the downloaded image:
B4X:
Sub Process_Globals
   Private bmp As Bitmap
End Sub

Sub Globals
   Dim ImageView1 As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("1")
 If bmp.IsInitialized = False Then
   Dim job As HttpJob
   job.Initialize("image", Me)
   job.Download("http://ciginfo.alwaysdata.net/img_sites/Img1.jpg")
End If
End Sub

Sub JobDone(Job As HttpJob)
   If Job.Success Then
      bmp = Job.GetBitmap
      ImageView1.SetBackgroundImage(bmp)
   End If
   Job.Release
End Sub

Sub Activity_Resume
   If bmp.IsInitialized Then ImageView1.Bitmap = bmp
End Sub
 
Upvote 0
Top