2

Android Api を使用して VCard 形式で Android デバイスの連絡先を読み込もうとしています。同じリンクが 1 つ見つかりました: Android contatcs vcard API

同じコードを書き込もうとしていますが、ルックアップキーを取得できないため、機能しません。

ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);  
int num = cur.getCount();  // I get 2 , as there are two contacts

String lookupKey = cur.getString(cur.getColumnIndex(Contacts.LOOKUP_KEY));
// The above line gives error : android.database.CursorIndexOutOfBoundsException:
//  Index -1 requested, with a size of 2

Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
AssetFileDescriptor fd = resolver.openAssetFileDescriptor(uri, "r");
FileInputStream fis = fd.createInputStream();
    byte[] b = new byte[(int)fd.getDeclaredLength()];
fis.read(b);
String vCard = new String(b);
sb.append(vCard);

上記のコードのルックアップキーを取得する方法を教えてください。または、Android api を使用して連絡先の VCard 形式を取得する他の方法があります。

4

3 に答える 3

7

はい、どうぞ:

Cursor cursor = context.getContentResolver().query(
                ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

if (cursor != null && cursor.moveToFirst()) {
    try {
        do {
            String lookupKey = cursor.getString(cursor
                    .getColumnIndex(Contacts.LOOKUP_KEY));


            Uri uri = Uri.withAppendedPath(
                    ContactsContract.Contacts.CONTENT_VCARD_URI,
                    lookupKey);
            AssetFileDescriptor fd;
            try {
                fd = context.getContentResolver()
                        .openAssetFileDescriptor(uri, "r");
                FileInputStream fis = fd.createInputStream();
                byte[] b = new byte[(int) fd.getDeclaredLength()];
                fis.read(b);
                String vCard = new String(b);
                Log.i(TAG, vCard);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } while (cursor.moveToNext());
    } finally {
        cursor.close();
    }
}
于 2012-03-08T10:17:47.467 に答える
1

-編集2---

カーソルを読み取る前にcur.moveToFirst()を実行していないように見えるため、例外が発生しています。android.database.CursorIndexOutOfBoundsExceptionを調べてみてください:同じ問題を説明するインデックス-1が要求されました。

-編集された回答-

コード内のLookUpKeyは、特定の連絡先用です。使用しているcondeの例は、特定の連絡先のvcardを取得することです。利用可能な連絡先をループする必要があり、内部にコードを配置して機能させることができます。連絡先契約からルックアップキーを取得できます。

それとは別に、一般的な解決策に従うことを検討する必要があります。

  1. 入ったエラーを追加してくださいlogcat
  2. アプリケーションが連絡先を読み取れるように、アプリケーションマニフェストに連絡先権限を追加しましたか?
于 2011-11-07T11:34:06.237 に答える
0
public static void getVCF() {
    final String vfile = "Contacts.csv";
    Cursor phones = mContext.getContentResolver().query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
            null, null);
    phones.moveToFirst();

    do {
        String lookupKey = phones.getString(phones
                .getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));

        Uri uri = Uri.withAppendedPath(
                ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
        AssetFileDescriptor fd;
        try {
            fd = mContext.getContentResolver().openAssetFileDescriptor(uri,
                    "r");
            FileInputStream fis = fd.createInputStream();
            byte[] buf = new byte[(int) fd.getDeclaredLength()];
            fis.read(buf);
            String VCard = new String(buf);
            String path = Environment.getExternalStorageDirectory()
                    .toString() + File.separator + vfile;
            FileOutputStream mFileOutputStream = new FileOutputStream(path,
                    true);
            mFileOutputStream.write(VCard.toString().getBytes());
            phones.moveToNext();
            Log.d("Vcard", VCard);
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    } while (phones.moveToNext());

}
于 2014-04-03T05:39:04.883 に答える