Wednesday, February 25, 2015

ANDROID code to get all Contacts information

    private void readContacts() {

        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
                null, null, null);
        String phone = null;
        // String emailContact = null;
        // String emailType = null;
        // String image_uri = "";
        // Bitmap bitmap = null;
        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                String id = cur.getString(cur
                        .getColumnIndex(ContactsContract.Contacts._ID));
                String name = cur
                        .getString(cur
                                .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                /*
                 * image_uri = cur .getString(cur
                 * .getColumnIndex(ContactsContract
                 * .CommonDataKinds.Phone.PHOTO_URI));
                 */
                if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                    // System.out.println("name : " + name + ", ID : " + id);
                    // sb.append(name+" "+id);
                    Cursor pCur = cr.query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                    + " = ?", new String[] { id }, null);
                    while (pCur.moveToNext()) {
                        phone = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        // sb.append(" " + phone);
                        // System.out.println("phone" + phone);
                    }
                    pCur.close();
                    StringBuilder pn = new StringBuilder();
                    for (char ch : phone.toCharArray()) {
                        if (ch == ' ' || ch == '\n') {
                        } else {
                            pn.append(ch);
                        }
                    }
                    FileData fileData = new FileData();
                    fileData.setName(name);
                    fileData.setPath(pn.toString());
                    fileData.setType(EApplicationConstant.Contact.val());
                    contactsList.add(fileData);
                    /*contactsList.add(name
                            + EApplicationConstant.DataSeparator.val() + " "
                            + pn.toString());*/

                    // sb.toString()

                    /*
                     * Cursor emailCur = cr.query(
                     * ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                     * ContactsContract.CommonDataKinds.Email.CONTACT_ID +
                     * " = ?", new String[] { id }, null); while
                     * (emailCur.moveToNext()) { emailContact = emailCur
                     * .getString(emailCur
                     * .getColumnIndex(ContactsContract.CommonDataKinds
                     * .Email.DATA)); emailType = emailCur .getString(emailCur
                     * .getColumnIndex
                     * (ContactsContract.CommonDataKinds.Email.TYPE));
                     * sb.append("\nEmail:" + emailContact + "Email type:" +
                     * emailType); System.out.println("Email " + emailContact +
                     * " Email Type : " + emailType);
                     *
                     * }
                     */

                    // emailCur.close();
                }

                /*
                 * if (image_uri != null) {
                 * System.out.println(Uri.parse(image_uri)); try { bitmap =
                 * MediaStore.Images.Media .getBitmap(this.getContentResolver(),
                 * Uri.parse(image_uri)); sb.append("\n Image in Bitmap:" +
                 * bitmap); System.out.println(bitmap);
                 *
                 * } catch (FileNotFoundException e) { // TODO Auto-generated
                 * catch block e.printStackTrace(); } catch (IOException e) { //
                 * TODO Auto-generated catch block e.printStackTrace(); }
                 *
                 * }
                 */

                // sb.append("\n........................................");
            }

            // textDetail.setText(sb);
        }
    }

No comments:

Post a Comment