如何在Android中的.csv文件中逐行写入contactname和contactno?
我想从手机中获取联系人姓名和相应的联系人号码,并将其写入 .csv
文件中。每行将包含整个联系人列表中每个人的联系人姓名和联系电话号码。如何在.csv
文件中写入联系信息?
我编写了用于显示联系人姓名列的代码,但它仅显示一个联系人,这意味着它会覆盖现有联系人,所以更改新联系人姓名的行应该是什么技巧。
我的代码
public class contactlist extends Activity {
static String name;
static int count;
static int countno;
static File file = null ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button filterbtn = (Button)findViewById(R.id.button1);
filterbtn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Uri u1 = Uri.fromFile(file);
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Person Details");
sendIntent.putExtra(Intent.EXTRA_STREAM, u1);
sendIntent.setType("text/html");
startActivity(sendIntent);
}
});
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));
name = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String columnString = "\"PersonName\",\"phoneno\"";
String dataString = null;
仅使用名称,“phoneno”是硬编码的。但在 .csv 文件中仅显示一个联系人
dataString = "\"" + name +"\",\"" + "phoneno" + "\"";
String combinedString = columnString + "\n" + dataString;
File root = Environment.getExternalStorageDirectory();
if (root.canWrite()){
File dir = new File (root.getAbsolutePath() + "/PersonData");
dir.mkdirs();
file = new File(dir, "Data.csv");
FileOutputStream out = null;
try {
out = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
out.write(combinedString.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
cur.close();
}
I want to take contactname and corresponding contact no from mobile and want to write it in .csv
file. Each row will contains contact name and contact number of each person for the entire contact list. How to write contact information in .csv
file?
I write the code for showing contactname column but it display only one contact means its override with existing one so what should be the trick for changing the row for new contact name.
My code
public class contactlist extends Activity {
static String name;
static int count;
static int countno;
static File file = null ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button filterbtn = (Button)findViewById(R.id.button1);
filterbtn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Uri u1 = Uri.fromFile(file);
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Person Details");
sendIntent.putExtra(Intent.EXTRA_STREAM, u1);
sendIntent.setType("text/html");
startActivity(sendIntent);
}
});
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));
name = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String columnString = "\"PersonName\",\"phoneno\"";
String dataString = null;
I use only name and "phoneno" is hardcoded. But in .csv file only one contact is shown
dataString = "\"" + name +"\",\"" + "phoneno" + "\"";
String combinedString = columnString + "\n" + dataString;
File root = Environment.getExternalStorageDirectory();
if (root.canWrite()){
File dir = new File (root.getAbsolutePath() + "/PersonData");
dir.mkdirs();
file = new File(dir, "Data.csv");
FileOutputStream out = null;
try {
out = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
out.write(combinedString.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
cur.close();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
已编辑。
这是 CSVWriter 类
并添加权限以体现为
Edited.
And here is CSVWriter class
And add permissions to manifest as