我正在一个应用程序中工作,我必须从我的设备中获取联系人及其详细信息。
我使用以下查询从联系人处获取电子邮件ID。
Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null, ContactsContract.Data._ID+ " = ?", new String[] { id }, null);
在这条线上发生撞击。
完整如下所述:
private void displayContacts() {
ContentResolver cr = RegistrationScreen.this.getContentResolver();
Cursor cur = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
System.out.println("has numebr"+(cur.getString(cur.getColumnIndex(ContactsContract.Data.HAS_PHONE_NUMBER))));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Data.HAS_PHONE_NUMBER))) > 0) {
Bean bean = new Bean();
String id = cur.getString(cur.getColumnIndex(ContactsContract.Data._ID));
String name = cur.getString(cur .getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
String fnamename = cur.getString(cur .getColumnIndex(ContactsContract.Data.DATA2));
String lnamename = cur.getString(cur .getColumnIndex(ContactsContract.Data.DATA3));
System.out.println("FIRTSNAME AND LAST NAME"+fnamename+"-"+lnamename);
System.out.println("ContactId is="+id+"For name="+name);
// String isUserProfile =
// cur.getString(cur.getColumnIndex(ContactsContract.Contacts.IS_USER_PROFILE));
String photoUri = cur.getString(cur.getColumnIndex(ContactsContract.Data.PHOTO_THUMBNAIL_URI));
Bitmap my_btmp = BitmapFactory.decodeResource(getResources(),R.drawable.pic);
String file_name ="";
String file_Path="";
if (photoUri != null) {
Uri my_contact_Uri = Uri.parse(photoUri);
try {
file_name =SharedPref.getValue(RegistrationScreen.this, SharedPref.PREFERENCE_KEY_USER_ID)+"-"+
SharedPref.getValue(RegistrationScreen.this, SharedPref.PREFERENCE_KEY_IEMI_NO)+"-"+getCurretntTimeStam()+".PNG";
my_btmp = MediaStore.Images.Media.getBitmap(RegistrationScreen.this.getContentResolver(),my_contact_Uri);
file_Path=storeImage(my_btmp, file_name);
bean.setFile_name(file_name);
bean.setFilePath(file_Path);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// InputStream photo_stream =
// ContactsContract.Contacts.openContactPhotoInputStream(getActivity().getContentResolver(),my_contact_Uri);
// BufferedInputStream buf =new
// BufferedInputStream(photo_stream);
// my_btmp = BitmapFactory.decodeStream(buf);
}
Cursor phonecur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.Data._ID
+ " = ?", new String[] { id }, null);
System.out.println("phone count"+phonecur.getCount());
if(phonecur.getCount()>0){
while (phonecur.moveToNext()) {
String phoneNo = phonecur.getString(phonecur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
int type = phonecur.getInt(phonecur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
String customLabel = phonecur.getString(phonecur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL));
CharSequence phoneType = ContactsContract.CommonDataKinds.Phone.getTypeLabel(RegistrationScreen.this.getResources(),
type, customLabel);
tempArrayList.add(phoneNo);
labelArrayList.add(String.valueOf(phoneType));
}
Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null, ContactsContract.Data._ID
+ " = ?", new String[] { id }, null);
String noteWhere = ContactsContract.Data._ID + " = ? AND "+ ContactsContract.Data.MIMETYPE + " = ?";
String[] noteWhereParams = new String[] { id,ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE };
Cursor noteCur = cr.query(ContactsContract.Data.CONTENT_URI, null, noteWhere, noteWhereParams, null);
String addrWhere = ContactsContract.Data._ID + " = ? AND "+ ContactsContract.Data.MIMETYPE + " = ?";
String[] addrWhereParams = new String[] {id,ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE };
Cursor addrCur = cr.query(ContactsContract.Data.CONTENT_URI, null,
addrWhere, addrWhereParams, null);
String websiteWhere = ContactsContract.Data._ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] websiteWhereParams = new String[] { id,ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE };
Cursor websiteCur = cr.query(ContactsContract.Data.CONTENT_URI, null,
websiteWhere, websiteWhereParams, null);
Cursor connectionCur = cr.query(ContactsContract.RawContacts.CONTENT_URI,
new String[]{ContactsContract.RawContacts.ACCOUNT_NAME, ContactsContract.RawContacts.ACCOUNT_TYPE},
ContactsContract.Data._ID +"=?",
new String[]{String.valueOf(id)},
null);
if (connectionCur != null && connectionCur.getCount() >0)
{
while (connectionCur.moveToNext()) {
String accountName = connectionCur.getString(connectionCur.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_NAME));
String accountType = connectionCur.getString(connectionCur.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_TYPE));
if(accountType!=null)
{
if(accountType.contains("whatsapp"))
{
connectionArrayList.add(accountType);
//Toast.makeText(LoginScreen.this, "whatsapp == "+accountType, Toast.LENGTH_SHORT).show();
}
else if(accountType.contains("google"))
{
connectionArrayList.add(accountType);
}
else if(accountType.contains("facebook"))
{
connectionArrayList.add(accountType);
}
else if(accountType.contains("twitter"))
{
connectionArrayList.add(accountType);
}
else if(accountType.contains("linkedin"))
{
connectionArrayList.add(accountType);
}
}
}
}
String notes ="";
while (noteCur.moveToNext()) {
notes = noteCur
.getString(noteCur
.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE));
}
String address="";
while (addrCur.moveToNext()) {
String poBox = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX));
String street = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
String city = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
String state = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
String postalCode = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
String country = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
String type1 = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));
address = poBox + " " + street + " " + city + " "
+ state + " " + postalCode + " " + country + " " + type1;
}
String website="";
while (websiteCur.moveToNext()) {
website = websiteCur.getString(websiteCur.getColumnIndex(ContactsContract.CommonDataKinds.Website.DATA1));
}
while (emailCur.moveToNext()) {
// to get the contact names
String nameemail = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
Log.e("Name :", nameemail);
String email = emailCur .getString(emailCur .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
String emailTypeint = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
String customemailLabel = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL));
CharSequence emailType = ContactsContract.CommonDataKinds.Phone.getTypeLabel(RegistrationScreen.this.getResources(),
Integer.parseInt(emailTypeint), customemailLabel);
if (email != null) {
emailArrayList.add(email);
emailTypeArrayList.add(String.valueOf(emailType));//convert 12 into type
}
}
emailCur.close();
noteCur.close();
addrCur.close();
websiteCur.close();
//emailCur.close();
connectionCur.close();
// bean.setContactpic(my_btmp);
// bean.setContactNumber(contactNumber);
nameArrayList.add(bean);
}
}
}
}
这是在O.S.下面的工作。刚刚在5.0.2崩溃。以下是崩溃日志。
02-03 12:18:29.913: E/AndroidRuntime(14969): FATAL EXCEPTION: Thread-9391
02-03 12:18:29.913: E/AndroidRuntime(14969): Process: tv.w, PID: 14969
02-03 12:18:29.913: E/AndroidRuntime(14969): android.database.sqlite.SQLiteException: unable to open database file (code 14)
02-03 12:18:29.913: E/AndroidRuntime(14969): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:181)
02-03 12:18:29.913: E/AndroidRuntime(14969): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
02-03 12:18:29.913: E/AndroidRuntime(14969): at android.content.ContentProviderProxy.query(ContentProviderNative.java:420)
02-03 12:18:29.913: E/AndroidRuntime(14969): at android.content.ContentResolver.query(ContentResolver.java:478)
02-03 12:18:29.913: E/AndroidRuntime(14969): at android.content.ContentResolver.query(ContentResolver.java:422)
02-03 12:18:29.913: E/AndroidRuntime(14969): at tv.w.registration.RegistrationScreen.displayContacts(RegistrationScreen.java:859)
02-03 12:18:29.913: E/AndroidRuntime(14969): at tv.w.registration.RegistrationScreen.access$3(RegistrationScreen.java:750)
02-03 12:18:29.913: E/AndroidRuntime(14969): at tv.w.registration.RegistrationScreen$1$3.run(RegistrationScreen.java:410)
请建议。
答案 0 :(得分:0)
我希望这可以帮助你...... 我已经使用此代码工作正常。如果需要,还要检查This
public ArrayList<String> getNameEmailDetails(){
ArrayList<String> names = new ArrayList<String>();
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
Cursor cur1 = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (cur1.moveToNext()) {
//to get the contact names
String name=cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
if(email!=null){
names.add(email);
}
}
cur1.close();
}
}
return names;
}
谢谢,
拿烟</ P>