获取联系人并将其作为android中的vcf文件发送到服务器

时间:2019-03-11 08:04:57

标签: android import-contacts

我正在尝试将联系人转换为android中的.vcf文件,但出现错误,无法导出联系人。

它给我一个错误,它进入catch块并给出:

java.lang.NegativeArraySizeException: -1

我想使用vcf文件将所有联系人导出到服务器

public void getVCF() {
 final String vfile = "Contacts.vcf";
 Cursor phones = mContext.getContentResolver().query(
  ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
  null, null, null);
 phones.moveToFirst();
 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 = 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();
   filevcf = new File(path);
   Log.d("Vcard", VCard);
  } catch (Exception e1) {
   // TODO Auto-generated catch block
   e1.printStackTrace();
  }
 }
}

0 个答案:

没有答案