Search code examples
androidemailandroid-contactsandroid-cursorloader

Get owner's email address with CursorLoader


I'm trying to fill an ArrayList with the owner's emails. I'm doing this in a fragment. Here is my code :

 public class ItemDetailFragment extends Fragment implements
        LoaderManager.LoaderCallbacks<Cursor> {

    ArrayList<String> emails = new ArrayList<String>();

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getLoaderManager().initLoader(0, null, this);

    }


        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
                View myFragmentView = inflater.inflate(R.layout.formal_email_layout,
                                container, false);
                return myFragmentView;
       }


 public Loader<Cursor> onCreateLoader(int id, Bundle arguments) {
            return new CursorLoader(getActivity(),
                    // Retrieve data rows for the device user's 'profile' contact.
                    Uri.withAppendedPath(
                            ContactsContract.Profile.CONTENT_URI,
                            ContactsContract.Contacts.Data.CONTENT_DIRECTORY),
                    ProfileQuery.PROJECTION,

                    // Select only email addresses.
                    ContactsContract.Contacts.Data.MIMETYPE + " = ?",
                    new String[]{ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE},

                    // Show primary email addresses first. Note that there won't be
                    // a primary email address if the user hasn't specified one.
                    null);
        }

public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
            List<String> emails = new ArrayList<String>();
            cursor.moveToFirst();
            while (!cursor.isAfterLast()) {
                emails.add(cursor.getString(ProfileQuery.ADDRESS));
                // Potentially filter on ProfileQuery.IS_PRIMARY
                cursor.moveToNext();
            }

               Log.d("test", cursor.getString(ProfileQuery.ADDRESS));
            }


public void onLoaderReset(Loader<Cursor> cursorLoader) {
            }

            private interface ProfileQuery {
                String[] PROJECTION = {
                        ContactsContract.CommonDataKinds.Email.ADDRESS,
                        ContactsContract.CommonDataKinds.Email.IS_PRIMARY,
                };

                int ADDRESS = 0;
                int IS_PRIMARY = 1;
            }

    }

I've basically used the example given here, I've just adapted it to a fragment.

When the fragment is lunched, the app crashes and I get this error :

07-05 12:27:55.235: W/dalvikvm(23217): threadid=1: thread exiting with uncaught exception (group=0x40c2f1f8)
07-05 12:27:55.235: E/AndroidRuntime(23217): FATAL EXCEPTION: main
07-05 12:27:55.235: E/AndroidRuntime(23217): android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0

(I do have one Gmail account on my device)


Solution

  • Here's a little class to get owner email.

    You can use only the first part of code where you get account name (google synchronization email address).