-1

I tried to display contacts from phone in my app, But its showing both contacts from gmail and phone. But my emulator only shows phone contact not gmail contact.

How to avoid gmail contacts not to be selected. I need only contacts from phone.

Contacts from phone are:

contacts from phone

contacts showing in my app is,

contacts showing in my app

My code is,

private class ListViewContactsLoader extends AsyncTask<Void, Void, Cursor>{

    @Override
    protected Cursor doInBackground(Void... params) {
        Uri contactsUri = ContactsContract.Contacts.CONTENT_URI;

        // Querying the table ContactsContract.Contacts to retrieve all the contacts
        Cursor contactsCursor = getActivity().getContentResolver().query(contactsUri, null, null, null,
                ContactsContract.Contacts.DISPLAY_NAME + " ASC ");

        if(contactsCursor.moveToFirst()){
            do{
                long contactId = contactsCursor.getLong(contactsCursor.getColumnIndex("_ID"));

                Uri dataUri = ContactsContract.Data.CONTENT_URI;

                // Querying the table ContactsContract.Data to retrieve individual items like
                // home phone, mobile phone, work email etc corresponding to each contact
                Cursor dataCursor = getActivity().getContentResolver().query(dataUri, null,
                                        ContactsContract.Data.CONTACT_ID + "=" + contactId,
                                        null, null);

                String displayName="";
                photoPath="" + R.drawable.noimage;
                byte[] photoByte=null;

                if(dataCursor.moveToFirst()){
                    // Getting Display Name
                    displayName = dataCursor.getString(dataCursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME ));
                    do{
                        // Getting Phone numbers
                        if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)){
                            number = dataCursor.getString(dataCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        }
                        // Getting Photo
                        if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)){
                            photoByte = dataCursor.getBlob(dataCursor.getColumnIndex("data15"));

                            if(photoByte != null) {
                                Bitmap bitmap = BitmapFactory.decodeByteArray(photoByte, 0, photoByte.length);

                                // Getting Caching directory
                                File cacheDirectory = getActivity().getBaseContext().getCacheDir();

                                // Temporary file to store the contact image
                                File tmpFile = new File(cacheDirectory.getPath() + "/wpta_"+contactId+".png");

                                // The FileOutputStream to the temporary file
                                try {
                                    FileOutputStream fOutStream = new FileOutputStream(tmpFile);

                                    // Writing the bitmap to the temporary file as png file
                                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOutStream);

                                    // Flush the FileOutputStream
                                    fOutStream.flush();

                                    //Close the FileOutputStream
                                    fOutStream.close();

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                                photoPath = tmpFile.getPath();
                            }
                        }
                    }while(dataCursor.moveToNext());

                    // Adding id, display name, path to photo and other details to cursor
                    mMatrixCursor.addRow(new Object[]{Long.toString(contactId), displayName, number, photoPath});

                }
            }while(contactsCursor.moveToNext());
        }
        return mMatrixCursor;
    }

How to avoid contacts from gmail. Is this possible.

Anthon
  • 69,918
  • 32
  • 186
  • 246
Jithin Varghese
  • 2,018
  • 3
  • 29
  • 56

1 Answers1

1

have to tried this Code:

TelephonyManager tMgr = (TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();

Required Permission:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

if you need a example then you can check out this http://examples.javacodegeeks.com/android/core/provider/android-contacts-example/

Abhishek
  • 2,350
  • 2
  • 21
  • 35