1

当我简单查询我的联系人时,列表中显示的所有联系人还包括 facebook 联系人和 Gmail 联系人,但是当我使用此代码时,一些 vcard 是空白的......

            String VCard = "";

         Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null, null, null);
         phones.moveToFirst();
         pDialog.setMax(phones.getCount());
           for(int i =0;i<phones.getCount();i++)
           {

              String lookupKey =  phones.getString(phones.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
              Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);

              AssetFileDescriptor fd;
             try 
             {
                 fd = getContentResolver().openAssetFileDescriptor(uri, "r");
                 FileInputStream fis = fd.createInputStream();
                 byte[] buf = new byte[(int) fd.getDeclaredLength()];
                 fis.read(buf);
                 VCard += new String(buf);
                 String y =String.valueOf(i);
                 publishProgress(y);
                 phones.moveToNext();                           
                 Log.d("Vcard",  VCard);
             } 
             catch (Exception e1) 
             {
                  // TODO Auto-generated catch block
                  e1.printStackTrace();
             }

         }

        try {
            FileOutputStream mFileOutputStream = new FileOutputStream(file, false);
              mFileOutputStream.write(VCard.toString().getBytes());
              mFileOutputStream.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
4

1 回答 1

2

ContactsContract.CommonDataKinds.Phone.CONTENT_URI 是手机的uri,您应该将其更改为 android.provider.ContactsContract.Contacts.CONTENT_URI 您将获得非空的联系人电子名片

于 2013-03-11T06:17:30.500 回答