Android Contact 导入导出 vcf格式(不依赖第三方库)
Android sdk 支持vcf处理的(忘记最低哪个版本开始支持的了,可以查一查)
备注:此代码来自Stack Overflow(原地址找不到了,o(╥﹏╥)o)
1. 导出联系人为vcf格式
Contact provider中有ContactsContract.Contacts.CONTENT_VCARD_URI的定义,说明他是支持vCard规范的
package com.mygmer.contactstest; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList; import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.ContactsContract;
import android.util.Log; public class VCardActivity extends Activity {
Cursor cursor;
ArrayList<String> vCard;
String vfile; /**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vfile = "Contacts" + "_" + "mfTest" + ".vcf";
/**This Function For Vcard And here i take one Array List in Which i store every Vcard String of Every Conatact
* Here i take one Cursor and this cursor is not null and its count>0 than i repeat one loop up to cursor.getcount() means Up to number of phone contacts.
* And in Every Loop i can make vcard string and store in Array list which i declared as a Global.
* And in Every Loop i move cursor next and print log in logcat.
* */
getVcardString(); } private void getVcardString() {
// TODO Auto-generated method stub
vCard = new ArrayList<String>();
cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
if (cursor != null && cursor.getCount() > 0) {
cursor.moveToFirst();
for (int i = 0; i < cursor.getCount(); i++) { get(cursor);
Log.d("TAG", "Contact " + (i + 1) + "VcF String is" + vCard.get(i));
cursor.moveToNext();
} } else {
Log.d("TAG", "No Contacts in Your Phone");
} } public void get(Cursor cursor) { //cursor.moveToFirst();
String lookupKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
AssetFileDescriptor fd;
try {
fd = this.getContentResolver().openAssetFileDescriptor(uri, "r"); // Your Complex Code and you used function without loop so how can you get all Contacts Vcard.?? /* 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 out = new FileOutputStream(path);
out.write(VCard.toString().getBytes());
Log.d("Vcard", VCard);*/ FileInputStream fis = fd.createInputStream();
byte[] buf = new byte[(int) fd.getDeclaredLength()];
fis.read(buf);
String vcardstring = new String(buf);
vCard.add(vcardstring); String storage_path = Environment.getExternalStorageDirectory().toString() + File.separator + vfile;
FileOutputStream mFileOutputStream = new FileOutputStream(storage_path, false);
mFileOutputStream.write(vcardstring.toString().getBytes()); } catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
2.导入vcf格式联系人
Example code:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File("/storage/sdcard0/contacts_20180106150125.vcf")),"text/x-vcard");
startActivity(intent);
关于Intent.ACTION_VIEW的描述,里面举例就是举的Contact,嘿嘿:
/**
* Activity Action: Display the data to the user. This is the most common
* action performed on data -- it is the generic action you can use on
* a piece of data to get the most reasonable thing to occur. For example,
* when used on a contacts entry it will view the entry; when used on a
* mailto: URI it will bring up a compose window filled with the information
* supplied by the URI; when used with a tel: URI it will invoke the
* dialer.
* <p>Input: {@link #getData} is URI from which to retrieve data.
* <p>Output: nothing.
*/
由上面的描述猜测,还原过程中会弹出提示框(会不会有啥确认按钮?),在Android5.1测试,有弹出窗,但没确认按钮,支持就导入联系人了。其他高版本未测试过
Android Contact 导入导出 vcf格式(不依赖第三方库)的更多相关文章
- Android 联系人导入导出(VCard格式)
之前在Android Contact 导入导出 vcf格式(不依赖第三方库)记录了一下依赖Android sdk中的功能导入导出联系人(第一次做java项目内容,有些地方的记录是否正确,暂时我也不知道 ...
- 使用PHPExcel导入导出excel格式文件
使用PHPExcel导入导出excel格式文件 作者:zccst 因为导出使用较多,以下是导出实现过程. 第一步,将PHPExcel的源码拷贝到项目的lib下 文件包含:PHPExcel.ph ...
- android implementation 依赖第三方库
依赖第三方库
- 运用NodeJs环境并依赖第三方库,框架等实现网站前后端分离报错问题及处理方法
运用NodeJs环境并依赖第三方库,框架等实现网站前后端分离报错问题及处理方法 问题一: SyntaxError: missing ) after argument list in .....\vie ...
- NPOI 2.1.1 系列(1) 使用NPOI读取 Excel文档 ;NpoiExcelHelper 导入导出 2003格式 2007格式的 Excel; Npoi 导出 xlsx 格式
下载地址 http://npoi.codeplex.com/releases 下面放一个 NPOIHelper 助手类吧,也不是我写的- NpoiExcelHelper 可以生成xlsx格式publi ...
- Bash中使用MySQL导入导出CSV格式数据[转]
转自: http://codingstandards.iteye.com/blog/604541 MySQL中导出CSV格式数据的SQL语句样本如下: select * from test_inf ...
- vue 移动端轻量日期组件不依赖第三方库
Vue版移动端日期选择组件 1.优点:不需要依赖其他第三方库,灵活可配置: 不需要依赖第三方组件的vue日期移动端组件 小轮子 轻量可复用: https://github.com/BeckReed ...
- oracle如何快速导入导出文本格式数据
导出工具:sqluldr2工具说明:sqluldr2再以安装oracle客户端的环境下下无需再安装其它软件,只需将对应的软件包拷贝至对应目录,即可运行导出数据导出示例:--linux环境导出示例:/d ...
- mysql导入导出.csv格式数据
window下导入数据: LOAD DATA INFILE "C:\\1.csv" REPLACE INTO TABLE demo CHARACTER SET gb2312 FIE ...
随机推荐
- 从Handler+Message+Looper源代码带你分析Android系统的消息处理机制
PS一句:不得不说CSDN同步做的非常烂.还得我花了近1个小时恢复这篇博客. 引言 [转载请注明出处:http://blog.csdn.net/feiduclear_up CSDN 废墟的树] 作为A ...
- amazeui中内置的web组件有哪些且如何用
amazeui中内置的web组件有哪些且如何用 一.总结 一句话总结: 1.组件还是jquery.js+amazeui.js,和插件一样,准确的说是amazeui.css+jquery.js+amaz ...
- WPF 针对数据源某个属性进行排序
原文:WPF 针对数据源某个属性进行排序 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/wanlong360599336/article/detai ...
- Android菜鸟的成长笔记(24)——Android中的振动器
在某些时候,程序需要启动系统振动器,比如手机静音时使用振动提示用户:再比如玩游戏时,当系统碰撞.爆炸时使用振动带给用户更逼真的体验等.总之,振动是除视频.声音之外的另一种"多媒体" ...
- Leetcode--easy系列1
近期開始刷Leetcode题目.花了一个星期先完毕了easy难度级别的题目,easy级别的题目思路比較简单,但不一定就直接AC了,主要是问题要考虑全然.特别是对特殊情况的处理. #6 ZigZag C ...
- win32命令行小程序获取指定文件夹或者目录下面的所有文件大小,文件数量,目录数量
#include <Windows.h> #include <stdio.h> #include <tchar.h> LARGE_INTEGER lgA ...
- bootstrap paginator使用简述
1.引入bootstrap-paginator.js(依赖jquery)2.html部分需要一个ul标签,接下来我们会把该标签设置为分页组件 3.js部分(核心) 注意事项: 1.通过onPageCl ...
- 【64.52%】【codeforces 697D】Puzzles
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- PAT练习题概览
PAT(pat.zju.edu.cn)是一个面向 C/C++程序的 Online Judge 系统.相比 ZOJ,HDOJ,POJ 等 ACM 题库,PAT 的题目非常基础,对于数据结构.算法的入门是 ...
- twemproxy架构分析——剖析twemproxy代码前编
twemproxy背景 在业务量剧增的今天,单台高速缓存服务器已经无法满足业务的需求, 而相较于大容量SSD数据存储方案,缓存具备速度和成本优势,但也存在数据安全性的挑战.为此搭建一个高速缓存服务器集 ...