Photo Contacts

droman

Member
Licensed User
Longtime User
In one of his tutorials, put the following code to get a photograph of a contact:

Dim photo As Bitmap
photo = Contact.GetPhoto

If you would like to set another picture. What should I do?

Sorry for my english.
 

AZKANSOY

Member
Licensed User
Longtime User
Thank you for quick reply Erel,
This is very important for me.

Can you check this address http://developer.android.com/reference/android/provider/ContactsContract.DisplayPhoto.html

this site say "Retrieving a full-size photo by photo file ID (see PHOTO_FILE_ID)" with this Usage example


B4X:
 public InputStream openDisplayPhoto(long photoFileId) {
    Uri displayPhotoUri = ContentUris.withAppendedId(DisplayPhoto.CONTENT_URI, photoKey);
    try {
        AssetFileDescriptor fd = getContentResolver().openAssetFileDescriptor(
            displayPhotoUri, "r");
        return fd.createInputStream();
    } catch (IOException e) {
        return null;
    }
}


I'm not converting to B4A this code.
 
Upvote 0

AZKANSOY

Member
Licensed User
Longtime User
Yes I was tried. ContactsUtils.GetPhoto is return thumbnail photo. I've tried everything. This code is my last chance. How to write this code in B4A.

B4X:
public InputStream openDisplayPhoto(long photoFileId) {
    Uri displayPhotoUri = ContentUris.withAppendedId(DisplayPhoto.CONTENT_URI, photoKey);
    try {
        AssetFileDescriptor fd = getContentResolver().openAssetFileDescriptor(
            displayPhotoUri, "r");
        return fd.createInputStream();
    } catch (IOException e) {
        return null;
    }
}
 
Upvote 0

AZKANSOY

Member
Licensed User
Longtime User
1. I did use ContentResolver for photo_key.

B4X:
Private PeopleProjection() As String = Array As String("times_contacted", "last_time_contacted", _
      "display_name", "has_phone_number", "starred", "_id", "photo_id")

Private cr As ContentResolver
cr.Initialize("cr")
...
...
Sub GetAllContacts
  Dim u As Uri
  u.Parse("content://com.android.contacts/contacts")
  cr.QueryAsync(u, PeopleProjection, "", Null, "")

End Sub

Sub CR_QueryCompleted (Success As Boolean, Crsr As Cursor)
  If Success = False Then
      Log(LastException)
  Else

      For i = 0 To Crsr.RowCount - 1
        Crsr.Position = i
            Log(Crsr.GetString("photo_id"))
            '------- How to use this photo_id  :)
      Next
      Crsr.Close
  End If
End Sub

2. I find that code snippet http://developer.android.com/reference/android/provider/ContactsContract.DisplayPhoto.html

I don't know java script. I Want to use B4A code. Thanks for helps.
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. Use ContactsUtils.
2. Add this sub to ContactsUtils class:
B4X:
Public Sub GetFullSizePhoto(id As Long) As Bitmap
   Dim bmp As Bitmap
   Dim crsr As Cursor = cr.Query(dataUri, Array As String("photo_uri"), "contact_id = ?", _
     Array As String(id), "")
   If crsr.RowCount > 0 Then
     crsr.Position = 0
     Dim photoUri As String = crsr.GetString("photo_uri")
     Dim jo As JavaObject
     jo.InitializeStatic("anywheresoftware.b4a.objects.streams.File")
     Dim In As InputStream = File.OpenInput(jo.GetField("ContentDir"), photoUri)
     bmp.Initialize2(In)
     In.Close
   End If
   crsr.Close
   Return bmp
End Sub
 
Upvote 0

AZKANSOY

Member
Licensed User
Longtime User
1. Use ContactsUtils.
2. Add this sub to ContactsUtils class:
B4X:
Public Sub GetFullSizePhoto(id As Long) As Bitmap
   Dim bmp As Bitmap
   Dim crsr As Cursor = cr.Query(dataUri, Array As String("photo_uri"), "contact_id = ?", _
     Array As String(id), "")
   If crsr.RowCount > 0 Then
     crsr.Position = 0
     Dim photoUri As String = crsr.GetString("photo_uri")
     Dim jo As JavaObject
     jo.InitializeStatic("anywheresoftware.b4a.objects.streams.File")
     Dim In As InputStream = File.OpenInput(jo.GetField("ContentDir"), photoUri)
     bmp.Initialize2(In)
     In.Close
   End If
   crsr.Close
   Return bmp
End Sub

I've tried and it was run perfectly. Thank you very much Erel. You are excellent. You did a great help to me.
 
Upvote 0
Top