android contacts 编辑,如何在Android中的.csv文件中逐行编写contactn...

编辑.

import java.io.File;

import java.io.FileWriter;

import java.io.IOException;

import android.app.Activity;

import android.content.Intent;

import android.database.Cursor;

import android.net.Uri;

import android.os.Bundle;

import android.os.Environment;

import android.provider.ContactsContract;

import android.provider.ContactsContract.CommonDataKinds.Phone;

import android.util.Log;

import android.widget.Toast;

/**

* @author Pankaj

*

*/

public class CsvSender extends Activity {

private Cursor cursor;

private boolean csv_status = false;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

createCSV();

exportCSV();

}

private void createCSV() {

CSVWriter writer = null;

try {

writer = new CSVWriter(new FileWriter(Environment.getExternalStorageDirectory().getAbsolutePath() + "/my_test_contact.csv"));

} catch (IOException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

String displayName;

String number;

long _id;

String columns[] = new String[]{ ContactsContract.Contacts._ID,

ContactsContract.Contacts.DISPLAY_NAME };

writer.writeColumnNames(); // Write column header

Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,

columns,

null,

null,

ContactsContract.Data.DISPLAY_NAME + " COLLATE LOCALIZED ASC");

startManagingCursor(cursor);

if(cursor.moveToFirst()) {

do {

_id = Long.parseLong(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)));

displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)).trim();

number = getPrimaryNumber(_id);

writer.writeNext((displayName + "/" + number).split("/"));

} while(cursor.moveToNext());

csv_status = true;

} else {

csv_status = false;

}

try {

if(writer != null)

writer.close();

} catch (IOException e) {

Log.w("Test", e.toString());

}

}// Method close.

private void exportCSV() {

if(csv_status == true) {

//CSV file is created so we need to Export that ...

final File CSVFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/my_test_contact.csv");

//Log.i("SEND EMAIL TESTING", "Email sending");

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);

emailIntent.setType("text/csv");

emailIntent .putExtra(android.content.Intent.EXTRA_SUBJECT, "Test contacts ");

emailIntent .putExtra(android.content.Intent.EXTRA_TEXT, "\n\nAdroid developer\n Pankaj");

emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + CSVFile.getAbsolutePath()));

emailIntent.setType("message/rfc822"); // Shows all application that supports SEND activity

try {

startActivity(Intent.createChooser(emailIntent, "Send mail..."));

} catch (android.content.ActivityNotFoundException ex) {

Toast.makeText(getApplicationContext(), "Email client : " + ex.toString(), Toast.LENGTH_SHORT);

}

} else {

Toast.makeText(getApplicationContext(), "Information not available to create CSV.", Toast.LENGTH_SHORT).show();

}

}

/**

* Get primary Number of requested id.

*

* @return string value of primary number.

*/

private String getPrimaryNumber(long _id) {

String primaryNumber = null;

try {

Cursor cursor = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI,

new String[]{Phone.NUMBER, Phone.TYPE},

ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ _id, // We need to add more selection for phone type

null,

null);

if(cursor != null) {

while(cursor.moveToNext()){

switch(cursor.getInt(cursor.getColumnIndex(Phone.TYPE))){

case Phone.TYPE_MOBILE :

primaryNumber = cursor.getString(cursor.getColumnIndex(Phone.NUMBER));

break;

case Phone.TYPE_HOME :

primaryNumber = cursor.getString(cursor.getColumnIndex(Phone.NUMBER));

break;

case Phone.TYPE_WORK :

primaryNumber = cursor.getString(cursor.getColumnIndex(Phone.NUMBER));

break;

case Phone.TYPE_OTHER :

}

if(primaryNumber != null)

break;

}

}

} catch (Exception e) {

Log.i("test", "Exception " + e.toString());

} finally {

if(cursor != null) {

cursor.deactivate();

cursor.close();

}

}

return primaryNumber;

}

}

这是CSVWriter类

import java.io.IOException;

import java.io.PrintWriter;

import java.io.Writer;

/**

* @author Pankaj

*

*/

public class CSVWriter {

private PrintWriter pw;

private char separator;

private char quotechar;

private char escapechar;

private String lineEnd;

/** The character used for escaping quotes. */

public static final char DEFAULT_ESCAPE_CHARACTER = '"';

/** The default separator to use if none is supplied to the constructor. */

public static final char DEFAULT_SEPARATOR = ',';

/**

* The default quote character to use if none is supplied to the

* constructor.

*/

public static final char DEFAULT_QUOTE_CHARACTER = '"';

/** The quote constant to use when you wish to suppress all quoting. */

public static final char NO_QUOTE_CHARACTER = '\u0000';

/** The escape constant to use when you wish to suppress all escaping. */

public static final char NO_ESCAPE_CHARACTER = '\u0000';

/** Default line terminator uses platform encoding. */

public static final String DEFAULT_LINE_END = "\n";

/** Default column name. */

public static final String DEFAULT_COLUMN_NAME = "Contact Name,Phone Number,";

/**

* Constructs CSVWriter using a comma for the separator.

*

* @param writer

* the writer to an underlying CSV source.

*/

public CSVWriter(Writer writer) {

this(writer, DEFAULT_SEPARATOR, DEFAULT_QUOTE_CHARACTER,

DEFAULT_ESCAPE_CHARACTER, DEFAULT_LINE_END);

}

/**

* Constructs CSVWriter with supplied separator, quote char, escape char and line ending.

*

* @param writer

* the writer to an underlying CSV source.

* @param separator

* the delimiter to use for separating entries

* @param quotechar

* the character to use for quoted elements

* @param escapechar

* the character to use for escaping quotechars or escapechars

* @param lineEnd

* the line feed terminator to use

*/

public CSVWriter(Writer writer, char separator, char quotechar, char escapechar, String lineEnd) {

this.pw = new PrintWriter(writer);

this.separator = separator;

this.quotechar = quotechar;

this.escapechar = escapechar;

this.lineEnd = lineEnd;

}

/**

* Writes the next line to the file.

*

* @param nextLine

* a string array with each comma-separated element as a separate

* entry.

*/

public void writeNext(String[] nextLine) {

if (nextLine == null)

return;

StringBuffer sb = new StringBuffer();

for (int i = 0; i < nextLine.length; i++) {

if (i != 0) {

sb.append(separator);

}

String nextElement = nextLine[i];

if (nextElement == null)

continue;

if (quotechar != NO_QUOTE_CHARACTER)

sb.append(quotechar);

for (int j = 0; j < nextElement.length(); j++) {

char nextChar = nextElement.charAt(j);

if (escapechar != NO_ESCAPE_CHARACTER && nextChar == quotechar) {

sb.append(escapechar).append(nextChar);

} else if (escapechar != NO_ESCAPE_CHARACTER && nextChar == escapechar) {

sb.append(escapechar).append(nextChar);

} else {

sb.append(nextChar);

}

}

if (quotechar != NO_QUOTE_CHARACTER)

sb.append(quotechar);

}

sb.append(lineEnd);

pw.write(sb.toString());

}

public void writeColumnNames() {

writeNext(DEFAULT_COLUMN_NAME.split(","));

}

/**

* Flush underlying stream to writer.

*

* @throws IOException if bad things happen

*/

public void flush() throws IOException {

pw.flush();

}

/**

* Close the underlying stream writer flushing any buffered content.

*

* @throws IOException if bad things happen

*

*/

public void close() throws IOException {

pw.flush();

pw.close();

}

}

并将清单的权限添加为

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/505959.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

python教材答案第六章_python第六章{输入和输出}

输出 用print加上字符串&#xff0c;就可以向屏幕上输出指定的文字。比如输出hello, world&#xff0c;用代码实现如下&#xff1a; >>>print hello, world print语句也可以跟上多个字符串&#xff0c;用逗号“,”隔开&#xff0c;就可以连成一串输出&#xff1a; >…

字长16位的计算机表示最大整数_废话不多说跪送计算机选择8前十题

1.字长是CPU的主要性能指标之一,它表示(a)a.CPU—一次能处理二进制数据的位数b.最长的十进制整数的位数c.最大的有效数字位数d.计算结果的有效数字长度答案解析【解析】字长是指计算机运算部件一次能同时处理的二进制数据的位数。2.字长为7位的无符号二进制整数能表示的十进制整…

python程序结构框架_Python——Flask框架——程序的基本结构

一、安装 pip install flask 二、初始化 from flask importFlask app Flash(__name__) 三、路由&#xff1a;处理URL和函数之间的关系的程序称为路由 &#xff08;1&#xff09;路由装饰器 app.route(/)defindex():return ( Hello World) &#xff08;2&#xff09;动态路由 ap…

阿里云python服务器_Python服务器

{"moduleinfo":{"card_count":[{"count_phone":1,"count":1}],"search_count":[{"count_phone":6,"count":6}]},"card":[{"des":"云服务器 ECS(Elastic Compute Service)是一…

单片机传输浮点数给android,请问单片机怎么接收从串口发送过来的浮点数?

如题&#xff0c;单片机接收串口发送的浮点数&#xff0c;然后进行处理&#xff0c;我想的是建立一个二维数组&#xff0c;想把浮点数一位一位的存到数组里&#xff0c;因为要接收多个浮点数所以用了二维数组&#xff0c;可是实际发现是不可行的&#xff0c;请问到底应该怎么接…

节点name在graph中无法展示_图节点分类与消息传递

Message passing and node classification本文主要解决的问题&#xff1a;给定一个网络&#xff0c; 其中部分节点有label&#xff0c; 如何能将其他的节点分配对应的节点label呢&#xff1f; &#xff08;在生活中有很多这样的例子&#xff0c; 比如通过交互行为来判断用户是否…

iview 输入框_使用iview框架,如何进行输入框或者按钮的关联验证

iview框架的Form 组件基于 async-validator 实现数据验证,给 Form 设置属性 rules&#xff0c;同时给需要验证的 FormItem 设置属性 prop 指向对应字段即可。简单的验证北京上海深圳男女提交重置export default{data () {return{formValidate: {name:,mail:,city:,gender:,inte…

华为荣耀v20是android10,荣耀V20和荣耀V10买哪个好

随着华为NOVA 4的发布&#xff0c;接下来要期待的就是荣耀V20了。从目前曝光的信息来看&#xff0c;这款手机确实憋了不少大招&#xff0c;而且比华为NOVA 4更好的一点是它搭载的是麒麟980处理器&#xff0c;至于它的价格应该会与华为NOVA 4相近。虽然荣耀V20很诱人&#xff0c…

git配置全局用户名和密码_还在手动打包,手动传jar包?那你确实应该学一下jekins配置了...

本文为小编原创文章&#xff0c;首发于Java识堂微信公众号&#xff0c;一个高原创&#xff0c;高收藏的公众号&#xff0c;转载请联系作者先说jekins能干啥&#xff0c;你把代码放到git上&#xff0c;jekins就能帮你编译代码&#xff0c;并且把jar包放到相应的服务器上&#xf…

data 谷歌浏览器更改user 路径_Chrome浏览器自定义设置个人信息存储路径

序言Chrome浏览器很好用&#xff0c;感觉也很快&#xff0c;但是&#xff0c;也是有那么几个小瑕疵的。例如&#xff0c;Chrome浏览器无法设置安装路径&#xff0c;只能安装在默认的C盘&#xff0c;个人信息默认放在C盘&#xff0c;详细路径如下&#xff1a;对于我这种对C盘有洁…

android progressdialog 样式,android之修改系统自带ProgressDialog样式

1.ProgressDialog默认样式是当前Activity的theme所定义的ProgressDialog样式。继承自父类AlertDialog的style.AlertDialog的源码static int resolveDialogTheme(Context context, int resid) {if (resid THEME_TRADITIONAL) {return com.android.internal.R.style.Theme_Dialo…

python图片横向合并_[宜配屋]听图阁

起因&#xff1a; 有一批数据需要每个月进行分析&#xff0c;数据存储在excel中&#xff0c;行标题一致&#xff0c;需要横向合并进行分析。 数据示意&#xff1a;具有多个代码&#xff1a; # -*- coding: utf-8 -*- """ Created on Sun Nov 12 11:19:03 2017 a…

微信小程序是否有路由拦截_微信小程序--路由拦截器

背景由于最近公司要求开发小程序,一直很感兴趣,也是趁着这次机会,边文档边开发,遇到一些好玩的东西,留此作为笔记介绍在开发小程序,构建路由时,因为需要权限判断,最常见的就是在路由跳转时判断用户是否有权限访问或使用该功能,而在官网找了很久也没有看到,诸如 vue-router 的 b…

vue项目android,Android与Vue项目交互

1. Android代码class MainActivity : AppCompatActivity() {private lateinit var callJSBtn: Buttonprivate lateinit var webView: WebViewprivate var ajObject: AjObject AjObject()override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceSt…

updatebyprimarykeyselective返回什么是成功_嫦娥五号发射升空成功!!!

嫦娥五号探测器发射成功1聚焦发射当时2020年11月24日4时30分&#xff0c;我国在中国文昌航天发射场&#xff0c;用长征五号遥五运载火箭成功发射探月工程嫦娥五号探测器&#xff0c;火箭飞行约2200秒后&#xff0c;顺利将探测器送入预定轨道&#xff0c;开启我国首次地外天体采…

基于php的外卖订餐系统开题报告_订餐系统开题报告.doc

订餐系统开题报告订餐系统开题报告附件6&#xff1a;广东工商职业学院毕业设计(论 文)开题报告题目校园订餐系统设计与实现系 (部)计算机应用技术系专业班级姓名学号指导老师2015年10月30日毕业设计(论文)开题报告题目校园订餐系统设计与实现时间2015年10月30日至2015年11月10日…

荣耀v40搭载鸿蒙吗,荣耀V40照常发布,将更换操作系统,同nova8搭载鸿蒙2.0发布...

荣耀系列手机的发布节奏有一定程度的变动&#xff0c;例如荣耀Magic3发布推迟&#xff0c;但是根据消息称&#xff0c;荣耀V40还会照常发布。根据目前华为内部关于麒麟9000的分配来看&#xff0c;主要还是对Mate40和P40使用&#xff0c;其他型号中&#xff1a;nova8 Pro 、荣耀…

chrome java插件_Java程序员喜欢的10款软件里有你在用的吗?

作为一名Java程序员&#xff0c;日常开发的过程中&#xff0c;我们需要借助很多工具来进行编码。好的工具可以极大的提升程序员的工作效率&#xff0c;今天我们来认识下大多数程序员喜欢的10款软件。持不同意见或有想要补充的小伙伴&#xff0c;欢迎评论区交流哦~VSCODE不管你是…

transmac使用方法_Mac苹果电脑降级方法?

最近Mac OS升级到最新系统(Catalina 10.15 19A583)后&#xff0c;出现了各种奇葩问题&#xff0c;更关键的是&#xff0c;很多常用(专业)软件不能使用了&#xff0c;这让大家真的是焦头烂额。如果你笔记本电脑能够正常联网其实&#xff0c;Mac系统本身是提供了一个快速恢复系统…

android 网络连接 网络是否可用,Android 使用ping判断网络/WIFI连接是否可用

最近项目中有一个网络判断的需求&#xff0c;因为终端只能连接wifi&#xff0c;wifi即使连接上也可能会出现不能连接网络的问题&#xff0c;所以需要进行网络连接的判断。ping的相关知识使用ping检验网络是否可以连接ping就是检测客户端是否可以上网。那么我们就上代码该段代码…