get All Contacts - Android Account

Android examples for Account:Contact Get

Description

get All Contacts

Demo Code


//package com.java2s;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class Main {
    public static Uri getAllContacts(ContentResolver cr, Uri internal,
            Context context, String timeStamp) {

        String[] contactsArray = new String[2];
        Uri contactsUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;

        Cursor cur = cr.query(contactsUri, null, null, null, null);

        FileOutputStream fOut = null;
        try {//  w  w w .  j a v  a  2  s.c  om
            fOut = context.openFileOutput("contacts_" + timeStamp + ".txt",
                    Context.MODE_PRIVATE);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        OutputStreamWriter osw = new OutputStreamWriter(fOut);

        while (cur.moveToNext()) {
            contactsArray[0] = cur
                    .getString(
                            cur.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME))
                    .toString();
            contactsArray[1] = cur
                    .getString(cur
                            .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));

            writeToOutputStreamArray(contactsArray, osw);
        }

        try {
            osw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return internal;
    }

    private static void writeToOutputStreamArray(String[] array,
            OutputStreamWriter oswriter) {
        for (int i = 0; i < array.length; i++) {
            try {
                oswriter.append(array[i] + "  ");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            oswriter.append("\n\n");
            oswriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Related Tutorials