我使用ContactsContract
将联系人导出到联系人应用。一切都在其他所有设备上运行良好,但在索尼Xperia手机上我无法保存我已放入ArrayList<ContentValues> data
的数据。我的代码如下所示:
ArrayList<ContentValues> data = new ArrayList<ContentValues>();
//Add photo
if (userImageBitmap != null) {
ContentValues photoRow = new ContentValues();
photoRow.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
photoRow.put(ContactsContract.CommonDataKinds.Photo.PHOTO, bitmapToByteArray(userImageBitmap));
data.add(photoRow);
}
//Add phone home number
if (contact.getUserPhoneLandline() != null) {
ContentValues userLandlinePhoneRow = new ContentValues();
userLandlinePhoneRow.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
userLandlinePhoneRow.put(ContactsContract.CommonDataKinds.Phone.NUMBER, contact.getUserPhoneLandline());
userLandlinePhoneRow.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_HOME);
data.add(userLandlinePhoneRow);
}
//Add phone mobile number
if (contact.getUserPhoneMobile() != null) {
ContentValues userMobilePhoneRow = new ContentValues();
userMobilePhoneRow.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
userMobilePhoneRow.put(ContactsContract.CommonDataKinds.Phone.NUMBER, contact.getUserPhoneMobile());
userMobilePhoneRow.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);
data.add(userMobilePhoneRow);
}
//Add user work fax number
if (contact.getCompanyFax() != null) {
ContentValues faxRow = new ContentValues();
faxRow.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
faxRow.put(ContactsContract.CommonDataKinds.Phone.NUMBER, contact.getCompanyFax());
faxRow.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_FAX_WORK);
data.add(faxRow);
}
//Add user stationary work phone
if (contact.getCompanyPhoneLandline() != null) {
ContentValues stationaryWorkPhoneRow = new ContentValues();
stationaryWorkPhoneRow.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
stationaryWorkPhoneRow.put(ContactsContract.CommonDataKinds.Phone.NUMBER, contact.getCompanyPhoneLandline());
stationaryWorkPhoneRow.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_WORK);
data.add(stationaryWorkPhoneRow);
}
//Add user mobile work phone
if (contact.getCompanyPhoneMobile() != null) {
ContentValues mobileWorkPhoneRow = new ContentValues();
mobileWorkPhoneRow.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
mobileWorkPhoneRow.put(ContactsContract.CommonDataKinds.Phone.NUMBER, contact.getCompanyPhoneMobile());
mobileWorkPhoneRow.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_WORK_MOBILE);
data.add(mobileWorkPhoneRow);
}
//Add company email
if (contact.getUserEmail() != null) {
ContentValues emailHomeRow = new ContentValues();
emailHomeRow.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE);
emailHomeRow.put(ContactsContract.CommonDataKinds.Email.ADDRESS, contact.getUserEmail());
emailHomeRow.put(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_HOME);
data.add(emailHomeRow);
}
if (contact.getCompanyEmail() != null) {
ContentValues emailWorkRow = new ContentValues();
emailWorkRow.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE);
emailWorkRow.put(ContactsContract.CommonDataKinds.Email.ADDRESS, contact.getCompanyEmail());
emailWorkRow.put(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK);
data.add(emailWorkRow);
}
//Add company website
if (contact.getCompanyWebsite() != null) {
ContentValues websiteWorkRow = new ContentValues();
websiteWorkRow.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE);
websiteWorkRow.put(ContactsContract.CommonDataKinds.Website.URL, contact.getCompanyWebsite());
websiteWorkRow.put(ContactsContract.CommonDataKinds.Website.TYPE, ContactsContract.CommonDataKinds.Website.TYPE_WORK);
data.add(websiteWorkRow);
}
Intent intent = new Intent(Intent.ACTION_INSERT, ContactsContract.Contacts.CONTENT_URI);
intent.putParcelableArrayListExtra(ContactsContract.Intents.Insert.DATA, data);
//Add company info
if (contact.getCompanyName() != null) {
intent.putExtra(ContactsContract.Intents.Insert.COMPANY, contact.getCompanyName());
}
//Add title info
if (contact.getUserCorporateTitle() != null) {
intent.putExtra(ContactsContract.Intents.Insert.JOB_TITLE, contact.getUserCorporateTitle());
}
//Add address info
StringBuilder addressBuilder = new StringBuilder();
if (contact.getCompanyAddress() != null) {
addressBuilder.append(contact.getCompanyAddress());
}
if (contact.getCompanyPostcode() != null) {
addressBuilder.append(" ").append(contact.getCompanyPostcode());
}
if (contact.getCompanyCity() != null) {
addressBuilder.append(" ").append(contact.getCompanyCity());
}
if (contact.getCompanyCountry() != null) {
addressBuilder.append(" ").append(contact.getCompanyCountry());
}
intent.putExtra(ContactsContract.Intents.Insert.POSTAL, addressBuilder.toString());
intent.putExtra(ContactsContract.Intents.Insert.POSTAL_TYPE, ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK);
//Add contact name and surname
if (contact.getUserNameAndSurname() != null) {
intent.putExtra(ContactsContract.Intents.Insert.NAME, contact.getUserNameAndSurname());
}
//Start export contact activity
startActivityForResult(intent, CONTACT_SAVE_INTENT_REQUEST);
我直接添加到Intent(名称,公司等)的内容有效。但是通过intent.putParcelableArrayListExtra(ContactsContract.Intents.Insert.DATA, data);
保存的所有内容都没有在Sony Xperia(我不仅害怕Xperia系列)设备上显示。有什么解决办法吗?
答案 0 :(得分:-2)
确保您已授予阅读联系人权限。选择联系时,我有同样的问题。
<uses-permission android:name="android.permission.READ_CONTACTS" />