I am trying to read Android device contacts in VCard format using Android Api. i found one link for the same: Android contatcs vcard API
我正在尝试使用Android Api读取VCard格式的Android设备联系人。我找到了一个相同的链接:Android contatcs vcard API。
and trying to write the same code but its not working, as I am not able to get the lookupkey:
我试着写同样的代码,但它不起作用,因为我无法获得lookupkey:
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);
Can anyone please tell me how to get the lookupkey for the above code or there is any other way we can get contacts VCard format using Android api.
谁能告诉我如何获取上面代码的lookupkey,或者我们可以用Android api获取contact VCard格式。
3 个解决方案
#1
1
-- Edit 2 ---
——编辑2 - - - - - -
It looks like you are not doing cur.moveToFirst() before reading cursor, so you are getting exceptiion. try look into android.database.CursorIndexOutOfBoundsException: Index -1 requested which describes the same problem.
在读取游标之前,看起来您并没有执行cur.moveToFirst()操作,因此您将获得exceptiion。看着android.database试试。CursorIndexOutOfBoundsException:索引-1请求,描述相同的问题。
-- Edited answer --
——编辑回答
LookUpKey in the code is for a specific contact. The conde example you are using is to get the vcard for a specific contact. you have to loop though the available contacts and inside you can put the code you have to get it working. You can get look up key from contact contract.
代码中的查找键用于特定的联系人。您正在使用的conde示例是获取特定联系人的vcard。您必须循环遍历可用的联系人,并在其内部放置代码以使其工作。你可以从合同中获得查找键。
apart from that, you should consider following a general solution:
除此之外,你应该考虑采用一个通用的解决方案:
- Please add error that you got in
logcat
- 请添加您在logcat中获得的错误
- have you added contact permission in application manifest to allow application to read contacts?
- 您是否在应用程序清单中添加了联系*限以允许应用程序读取联系人?
#2
7
Here you are:
给你:
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();
}
}
#3
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());
}
#1
1
-- Edit 2 ---
——编辑2 - - - - - -
It looks like you are not doing cur.moveToFirst() before reading cursor, so you are getting exceptiion. try look into android.database.CursorIndexOutOfBoundsException: Index -1 requested which describes the same problem.
在读取游标之前,看起来您并没有执行cur.moveToFirst()操作,因此您将获得exceptiion。看着android.database试试。CursorIndexOutOfBoundsException:索引-1请求,描述相同的问题。
-- Edited answer --
——编辑回答
LookUpKey in the code is for a specific contact. The conde example you are using is to get the vcard for a specific contact. you have to loop though the available contacts and inside you can put the code you have to get it working. You can get look up key from contact contract.
代码中的查找键用于特定的联系人。您正在使用的conde示例是获取特定联系人的vcard。您必须循环遍历可用的联系人,并在其内部放置代码以使其工作。你可以从合同中获得查找键。
apart from that, you should consider following a general solution:
除此之外,你应该考虑采用一个通用的解决方案:
- Please add error that you got in
logcat
- 请添加您在logcat中获得的错误
- have you added contact permission in application manifest to allow application to read contacts?
- 您是否在应用程序清单中添加了联系*限以允许应用程序读取联系人?
#2
7
Here you are:
给你:
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();
}
}
#3
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());
}