How to load a Contact photo into an Imageview?

peve9

Member
Licensed User
Longtime User
Hi everyone! I would like to know how to load a contact photo into an imageview or in a panel. :)

Thanks everyone
 

mangojack

Expert
Licensed User
Longtime User
peve .. have a read here .. there is an example to access a contacts photo , then just load to an imageview

B4X:
ImageView1.Bitmap = photo

Phone Library - Contacts

Cheers mj
 
Upvote 0

peve9

Member
Licensed User
Longtime User
Thanks, I was already able to do this, but I'm working with the CustomListview class so I find difficulties with the panel.
B4X:
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.
   Dim clv1 As CustomListView
   Dim Panel1 As Panel
   Dim listOfContacts As List
   Dim Contatti As Contacts
   Dim Contatto As  Contact
   Dim nome As String
   Dim iv As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   Activity.LoadLayout("1")
   clv1.Initialize(Me, "clv1")
   Panel1.AddView(clv1.AsView, 0, 0, Panel1.Width, Panel1.Height)
   clv1.DefaultTextBackgroundColor = Colors.DarkGray
   listOfContacts = Contatti.GetAll
   For i = 0 To listOfContacts.Size - 1
      Contatto = listOfContacts.Get(i)
      clv1.Add(CreateListItem("Item #" & i, clv1.AsView.Width, 150dip), 150dip, "Item #" & i )
   Next
End Sub
Sub CreateListItem(Text As String, Width As Int, Height As Int)As Panel
   
   Dim p As Panel
   p.Initialize("")
   p.Color = Colors.Black
   Dim photo As Bitmap
   photo = Contatto.GetPhoto
   iv.Initialize("")
   If photo<>Null Then
   iv.Bitmap=photo
   iv.SetLayout(0,0,100dip,100dip)
   End If
   Dim b As Button
   b.Initialize("button") 'all buttons click events will be handled with Sub Button_Click
   Dim chk As CheckBox
   chk.Initialize("")
   Dim lbl As Label
   lbl.Initialize("")
   lbl.Gravity = Bit.OR(Gravity.CENTER_VERTICAL, Gravity.LEFT)
   lbl.Text = Text
   lbl.TextSize = 16
   lbl.TextColor = Colors.White
   b.Text = "Click"
   p.AddView(iv, 100, 2dip, 150dip, Height - 4dip) 'view #0
   'p.AddView(b, 155dip, 2dip, 110dip, Height - 4dip) 'view #1
   'p.AddView(chk, 280dip, 2dip, 50dip, Height - 4dip) 'view #2
   Return p

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
as an extra maybe also dim a default 'No Image' bitmap ,so if a contact photo is unavailable you load the 'no_image' bitmap instead.

B4X:
Sub Globals
    Dim imgDefault as Bitmap

Sub Activity_Create(FirstTime As Boolean)            
   'set the default NoImage bmp
    imgDefault = LoadBitmap(File.DirAssets,"no_image.png")  'or other image 

'--------------------------------------------------
Sub CreateListItem(Text As String, Width As Int, Height As Int)As Panel
    
    Dim p As Panel
    p.Initialize("")     
    p.Color = Colors.Black 
    Dim photo As Bitmap
    photo = Contatto.GetPhoto
    Dim iv As ImageView    'add this   
    iv.Initialize("")
    
    If photo<>Null Then
      iv.Bitmap=photo
    Else
      iv.Bitmap = imgDefault
    End if      
    iv.SetLayout(0,0,100dip,100dip)
Cheers mj
 
Last edited:
Upvote 0
Top