使用Cursor android在gmail和手机联系人中显示联系人

时间:2015-06-28 03:56:30

标签: android android-contacts contactscontract

我尝试在我的应用中显示来自手机的联系人,但是它显示了来自Gmail和手机的联系人。但我的模拟器只显示电话联系而非gmail联系。

如何避免不选择gmail联系人。我只需要通过电话联系。

来自电话的联系人是:

contacts from phone

我的应用中显示的联系人是,

contacts showing in my app

我的代码是,

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;
    }

如何避免来自Gmail的联系人。这可能。

1 个答案:

答案 0 :(得分:1)

必须尝试这个 代码:

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

必需许可:

{{1}}

如果你需要一个例子,那么你可以看一下 http://examples.javacodegeeks.com/android/core/provider/android-contacts-example/