Android保存联系人到通讯录的方法
作者:zhonglunshun 时间:2023-01-28 16:44:40
上一篇文章讲了如何获取所有联系人,这篇文章就讲下怎么保存联系人数据到本机通讯录。这里我就假设你已经拿到了要保存的联系人数据。
因为是一个工具类,所以我这里就只给一个方法了,也是很简单,但是写的没有读取联系人的数据那么多,要保存更多其实看下如何读取的就会了。
直接上源码:
/**
* 添加联系人到本机
*
* @param context
* @param contact
* @return
*/
public static boolean addContact(Context context, ContactEntity contact) {
try {
ContentValues values = new ContentValues();
// 下面的操作会根据RawContacts表中已有的rawContactId使用情况自动生成新联系人的rawContactId
Uri rawContactUri = context.getContentResolver().insert(
RawContacts.CONTENT_URI, values);
long rawContactId = ContentUris.parseId(rawContactUri);
// 向data表插入姓名数据
String name = contact.getName();
if (name != "") {
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
values.put(StructuredName.GIVEN_NAME, name);
context.getContentResolver().insert(
ContactsContract.Data.CONTENT_URI, values);
}
// 向data表插入电话数据
String mobile_number = contact.getPhone1().get(0);
if (mobile_number != "") {
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Phone.NUMBER, mobile_number);
values.put(Phone.TYPE, Phone.TYPE_MOBILE);
context.getContentResolver().insert(
ContactsContract.Data.CONTENT_URI, values);
}
// 向data表插入Email数据
String email = contact.getEmail();
if (email != "") {
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);
values.put(Email.DATA, email);
values.put(Email.TYPE, Email.TYPE_WORK);
context.getContentResolver().insert(
ContactsContract.Data.CONTENT_URI, values);
}
// 向data表插入QQ数据
String qq = contact.getQq();
if (qq != "") {
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Im.CONTENT_ITEM_TYPE);
values.put(Im.DATA, qq);
values.put(Im.PROTOCOL, Im.PROTOCOL_QQ);
context.getContentResolver().insert(
ContactsContract.Data.CONTENT_URI, values);
}
// 向data表插入备注信息
String describe = contact.getDescribe();
if (describe != "") {
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Note.CONTENT_ITEM_TYPE);
values.put(Note.NOTE, describe);
context.getContentResolver().insert(
ContactsContract.Data.CONTENT_URI, values);
}
// 向data表插入头像数据
Bitmap sourceBitmap = BitmapFactory.decodeResource(
context.getResources(), R.drawable.icon);
final ByteArrayOutputStream os = new ByteArrayOutputStream();
// 将Bitmap压缩成PNG编码,质量为100%存储
sourceBitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
byte[] avatar = os.toByteArray();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Photo.CONTENT_ITEM_TYPE);
values.put(Photo.PHOTO, avatar);
context.getContentResolver().insert(
ContactsContract.Data.CONTENT_URI, values);
}
catch (Exception e) {
return false;
}
return true;
}
标签:android,联系人,通讯录
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
一篇文章让你彻底了解Java可重入锁和不可重入锁
2023-12-06 11:57:26
![](https://img.aspxhome.com/file/2023/1/125491_0s.png)
C#中FileStream的对比及使用方法
2023-10-17 21:09:41
![](https://img.aspxhome.com/file/2023/3/91233_0s.jpg)
Android超实用的Toast提示框优化分享
2022-12-20 08:36:32
Android中扫描多媒体文件操作详解
2022-06-02 09:06:53
android xml实现按钮的圆角、阴影效果及按下变化效果的实现代码
2022-07-30 23:23:12
![](https://img.aspxhome.com/file/2023/0/137800_0s.png)
JDK 7 新特性小结实例代码解析
2022-04-18 13:03:07
Java实现企业员工管理系统
2023-08-22 16:44:50
Java京东面试题之为什么HashMap线程不安全
2022-12-06 07:20:02
![](https://img.aspxhome.com/file/2023/5/65615_0s.png)
C++的函数与指针
2022-12-20 14:08:15
![](https://img.aspxhome.com/file/2023/7/125907_0s.png)
史上最全图文讲解Java泛型
2022-08-23 20:27:47
![](https://img.aspxhome.com/file/2023/4/62834_0s.png)
Java Valhalla Project项目介绍
2021-10-03 00:29:28
![](https://img.aspxhome.com/file/2023/0/83120_0s.png)
linux下c语言的多线程编程
2023-06-29 09:52:42
![](https://img.aspxhome.com/file/2023/8/128108_0s.jpg)
java 如何读取远程主机文件
2022-03-23 05:18:19
在idea中显示springboot面板的方法
2022-01-02 22:00:57
![](https://img.aspxhome.com/file/2023/6/71846_0s.png)
c#语言使用Unity粒子系统制作手雷爆炸
2021-10-11 11:13:46
![](https://img.aspxhome.com/file/2023/8/92918_0s.png)
log4j配置失效日志中打印Debug信息问题
2022-10-12 10:13:12
![](https://img.aspxhome.com/file/2023/7/100667_0s.png)
Java使用递归法解决汉诺塔问题的代码示例
2023-09-05 20:31:28
![](https://img.aspxhome.com/file/2023/1/64051_0s.jpg)
关于MyBatis中Mapper XML热加载优化
2023-05-20 01:49:34
![](https://img.aspxhome.com/file/2023/9/96529_0s.png)
深入学习C#网络编程之HTTP应用编程(下)
2023-03-16 12:06:37
![](https://img.aspxhome.com/file/2023/1/70411_0s.png)
Android开发中应用程序分享功能实例
2021-05-27 20:38:46