Android Question Create contact missing fields

ddahan

Member
Licensed User
Longtime User
ContactsUtils enables new contact creation. However, there are some fields that are not writable.

I'll appreciate your advice to find a way to enable update/Add the following contact fields:

- Organization

- Title

- Contact Photo

- Postal address


MiscUtil lib supposed to have these functionalities but I did not managed to make it working.


Thanks,

David.
 

ddahan

Member
Licensed User
Longtime User
Thanks for encouraging me to do it by myself.

I managed to implement methods for Add/Set Organization (Company,Title):

B4X:
Type cuOrganization(Company As String, Title As String)

Public Sub SetOrganization(ID As Long,Org As cuOrganization)
    Dim v As ContentValues
    v.Initialize
    v.PutString("data1", Org.Company)
    v.PutString("data4", Org.Title)
    SetData("vnd.android.cursor.item/organization", v, ID, True)
End Sub

Public Sub AddOrganization(ID As Long,Org As cuOrganization)
    Dim v As ContentValues
    v.Initialize
    v.PutString("data1", Org.Company)
    v.PutString("data4", Org.Title)
    SetData("vnd.android.cursor.item/organization", v, ID, False)
End Sub

Postal Address and Photo are not working yet and I'll appreciate your help.
The following is the code:

B4X:
Public Sub AddPostalAddress(Id As Long, PostalAddr As String, PostalAddrType As String)
    Dim v As ContentValues
    v.Initialize
    v.PutString("data1", PostalAddr)
    v.PutInteger("data2", GetKeyFromValue(PostalAddtrTypes, PostalAddrType, 2))
    SetData("vnd.android.cursor.item/StructuredPostal", v, Id, False)
End Sub

Sub AddPhoto(ID As Long, PhotoBitMap As Bitmap)
    Dim Out As OutputStream
    Dim bytes() As Byte
    Out.InitializeToBytesArray(1000)
    bytes = Out.ToBytesArray
    PhotoBitMap.WriteToStream(Out,100,"PNG")
    Out.Close
    Dim v As ContentValues
    v.Initialize
    v.PutBytes("data15", bytes)
    SetData("vnd.android.cursor.item/photo", v, ID, False)
End Sub

No errors found but not functional.
Have a great weekend.
Edit: Forgot WriteToStream, still not working ...
David.
 
Last edited:
Upvote 0

ddahan

Member
Licensed User
Longtime User
OK. Add/SetPhoto is working now!
This is the correct code:
B4X:
Public Sub AddPhoto(ID As Long, PhotoBitMap As Bitmap)
    Dim Out As OutputStream
    Dim bytes() As Byte 
    Out.InitializeToBytesArray(1000)
    PhotoBitMap.WriteToStream(Out,100,"PNG")
    bytes = Out.ToBytesArray
    Out.Close   
    Dim v As ContentValues
    v.Initialize
    v.PutBytes("data15", bytes)
    SetData("vnd.android.cursor.item/photo", v, ID, False)
End Sub

Public Sub SetPhoto(ID As Long, PhotoBitMap As Bitmap)
    Dim Out As OutputStream
    Dim bytes() As Byte 
    PhotoBitMap.WriteToStream(Out,100,"PNG")
    Out.InitializeToBytesArray(1000)
    bytes = Out.ToBytesArray
    Out.Close   
    Dim v As ContentValues
    v.Initialize
    v.PutBytes("data15", bytes)
    SetData("vnd.android.cursor.item/photo", v, ID, True)
End Sub
 
Upvote 0

ddahan

Member
Licensed User
Longtime User
Seems that all needed methods are working now.
This is the correct code for AddPostalAddress:
B4X:
Public Sub AddPostalAddress(Id As Long, PostalAddr As String, PostalAddrType As String)
    Dim v As ContentValues
    v.Initialize
    v.PutString("data1", PostalAddr)
    v.PutInteger("data2", GetKeyFromValue(PostalAddtrTypes, PostalAddrType, 2))
    SetData("vnd.android.cursor.item/postal-address_v2", v, Id, False)
End Sub

Erel, Thanks for your support.

David.
 

Attachments

  • ContactsUtils.bas
    14.2 KB · Views: 298
Upvote 0

ddahan

Member
Licensed User
Longtime User
Inserting a new contact with two words in first name using ContactsUtils.InsertContact creates a contact with first and middle name (instead of a two words first name).
It will be better to have detailed name components in the argument instead of display name but I cannot find a reference for contact name components in Android Developer API documentation.
I'll appreciate your advice.
 
Upvote 0

ddahan

Member
Licensed User
Longtime User
Thanks for your advice.
I have added the following type:
B4X:
    Type cuContactName(GivenName As String, FamilyName As String, MiddleName As String, Prefix As String)

the following methods have been added:
B4X:
Sub InsertContactWithDetailedName(ContactName As cuContactName, Phone As String) As cuContact
and
B4X:
Sub GetDetailedName(id As Long) As cuContactName

Attached updated ContactsUtils.

Thanks,
David.
 

Attachments

  • ContactsUtils.bas
    19.4 KB · Views: 297
Upvote 0
Top