Android Question How to add a photograph to a contact.

beacon

Member
Licensed User
Longtime User
B4A: I can add a new contact to the phone's contacts list successfully with ContactUtils and ContentResolver Ver 1.50.

Is there please a method to add a photograph to a contact?

Thank you very much.
 

Dogbonesix

Active Member
Licensed User
Longtime User
I added the Sub to ContactsUtils added the following code:
...
Dim b() As Byte = Bit.InputStreamToBytes(File.OpenInput(File.DirAssets, "up.png"))
cu.SetPhoto(121, b) ' 121 is a specific contact record in lieu of c.id

Msgbox("An instance of " & EditText1.Text & " was found in the Device and adjusted.","")
Activity.Finish
...

The code passes through ContactUtils but nothing happens - no picture is changed, no hits, no errors. What am I doing wrong? Thanks
 
Upvote 0

Francisco Picado

Member
Licensed User
Longtime User
the same happen to me, try to add a contact with the code but nothing, is there a chance that have to refresh something at the contact list to update the photo for the contact? if so how to do it?
 
Upvote 0

Dogbonesix

Active Member
Licensed User
Longtime User
Warning 1003 - 1003: Object was already initialized.

Excerpt from Manifest:

AddPermission("android.permission.READ_CONTACTS")
AddPermission("android.permission.WRITE_CONTACTS")

I don't see anything that would stop adding/replacing the Contact Photo as long as a correct ID is used. The Log is attached. Thanks
 

Attachments

  • Warning 1003.png
    Warning 1003.png
    5.9 KB · Views: 231
Upvote 0

Dogbonesix

Active Member
Licensed User
Longtime User
I simplified the code and found the UnFilter feature on the Log (DA).

Unfiltered Log:
ViewPostImeInputStage ACTION_DOWN
ViewPostImeInputStage ACTION_DOWN
sendUserActionEvent() mView == null

Code:
Sub Label10_Click
Dim b() As Byte = Bit.InputStreamToBytes(File.OpenInput(File.DirAssets, "up.png"))
cu.SetPhoto(79,b)
Msgbox("Photo Updated.","")
End Sub
 
Upvote 0

beacon

Member
Licensed User
Longtime User
For when my contact had no photo, I changed the SetPhoto code in ContactsUtils

SetData("vnd.android.cursor.item/photo", v, Id, True)

to

SetData("vnd.android.cursor.item/photo", v, Id, False)

I then called Setphoto in my own code with

cu.SetPhoto(k.Id, b, False)

which then worked for me, so it might be worth trying.

Good luck.
 
Last edited:
Upvote 0
Top