Android Question Owner accounts with all field

brelto85

Active Member
Licensed User
The first method return a owner account with some fields but there isn't a email
The second method list the accounts owner returns only a emails value

B4X:
Sub GetOwnerAccount() As Cursor
      Dim cr As ContentResolver
      Dim u As Uri
      u.Parse("content://com.android.contacts/profile")
      cr.Initialize("cr")
      Dim crsr As Cursor = cr.Query(u, Null, "", Null, "")
      crsr.Position = 0
      Return crsr
End Sub

Public Sub GetAccounts As List
    Dim r As Reflector
    Dim wAccounts As List
    wAccounts.Initialize
    r.Target = r.RunStaticMethod("android.accounts.AccountManager", "get", Array As Object(r.GetContext), Array As String("android.content.Context"))
    Dim accounts() As Object
    accounts = r.RunMethod2("getAccountsByType", "com.google", "java.lang.String")
    For i = 0 To accounts.Length - 1
          r.Target = accounts(i)
        wAccounts.Add(r.GetField ("name"))
      Next
    Return wAccounts
End Sub
How to merge?
I need to list the owner accounts recovering the email, name, surname and picture for each profile
 

Erel

Administrator
Staff member
Licensed User
This code will print the account name (as well as the other fields).
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim cr As ContentResolver
   Dim u As Uri
   u.Parse("content://com.android.contacts/profile/raw_contacts")
  cr.Initialize("cr")
  Dim crsr As Cursor = cr.Query(u, Null, "", Null, "")
   printCursor(crsr)
   crsr.Close
End Sub

Private Sub printCursor(c As Cursor) 'ignore
   For r = 0 To c.RowCount - 1
     c.Position = r
     For col = 0 To c.ColumnCount - 1
       Try
         Log(c.GetColumnName(col) & ": " & c.GetString2(col))
       Catch
         Log(c.GetColumnName(col) & ": " & LastException)
       End Try
     Next
     Log("***************")
   Next
End Sub
You will need to learn how profiles are stored in order to fetch the photo.
 

brelto85

Active Member
Licensed User
i try with different uri but the email field is not present

...
times_contacted: 0
phonetic_name: null
data_set: null
phonetic_name_style: 0
contact_id: 9223372034707292161
version: 43
last_time_contacted: null
aggregation_mode: 3
_id: 9223372034707292161
name_verified: 0
dirty: 1
display_name_source: 40
send_to_voicemail: 0
account_type: null
account_type_and_data_set: null
custom_ringtone: null
sync4: null
sync3: null
sync2: null
sync1: null
deleted: 0
account_name: null
display_name: Alberto Rubini
raw_contact_is_user_profile: 1
sort_key_alt: Rubini, Alberto
starred: 0
sort_key: Alberto Rubini
display_name_alt: Rubini, Alberto
sourceid: null
 

NJDude

Expert
Licensed User
I tried the code posted above and on KitKat the "account_name" is null, however, on ICS I can see the email address it seems there are differences between versions.

Devices: HTC One and Nexus 7 = KitKat, Galaxy Tab 10.1 = ICS
 

Cableguy

Expert
Licensed User
So I'm trying this one out, but I keep getting a permission denial

B4X:
java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/profile/raw_contacts from pid=24150, uid=10071 requires android.permission.READ_CONTACTS, or grantUriPermission()
I also added the following to the manifest:


B4X:
AddPermission
("android.permission.GET_ACCOUNTS")
AddPermission
("android.permission.READ_CONTACTS")
AddPermission
("grantUriPermission")
so... what am I missing??
 

NJDude

Expert
Licensed User
"grantUriPermission" is not a permission, just the previous 2, also, the permissions don't have to be enclosed in quotes.
 
Last edited:

Cableguy

Expert
Licensed User
"grantUriPermission" is not a permission, just the previous 2.
I have tried that, it always pauses at this line:

B4X:
Dim crsr AsCursor = cr.Query(u, Null, "", Null, "")
it gives in the logs a permission denial, but i cant figure ou what or why
 

Cableguy

Expert
Licensed User
Here you are...
I am using B4A Bridge Plus thus the CallSubDelayed
B4X:
LogCat connected to: B4A-Bridge: arnova ARNOVA 101 G4-
--------- beginning of /dev/log/main
** Activity (main) Create, isFirst = false **
** Activity (main) Resume **
WakeLock already held.
** Service (service1) Destroy **
** Service (service1) Create **
** Service (service1) Start **
Connected to B4A-Bridge (Wifi)
Installing file.
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (CallSubDelayed - HeartBeat)
PackageAdded: package:b4a.example
** Activity (main) Create, isFirst = true **
java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/profile/raw_contacts from pid=9559, uid=10071 requires android.permission.READ_CONTACTS, or grantUriPermission()
    at android.os.Parcel.readException(Parcel.java:1425)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:185)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
    at android.content.ContentProviderProxy.query(ContentProviderNative.java:366)
    at android.content.ContentResolver.query(ContentResolver.java:372)
    at android.content.ContentResolver.query(ContentResolver.java:315)
    at anywheresoftware.b4a.objects.ContentResolverWrapper.Query(ContentResolverWrapper.java:43)
    at b4a.example.main._activity_create(main.java:315)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:174)
    at b4a.example.main.afterFirstLayout(main.java:98)
    at b4a.example.main.access$100(main.java:16)
    at b4a.example.main$WaitForLayout.run(main.java:76)
    at android.os.Handler.handleCallback(Handler.java:725)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5041)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    at dalvik.system.NativeStart.main(Native Method)
running waiting messages (1)
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
My code is this thread example unchanged
 

NJDude

Expert
Licensed User
Like I said on my previous post, you should not enclose the permissions string in quotes (according to what you posted previously)
 
Top