我想提取联系人姓名并将其放入xml文件中 //第一个提取联系人的方法
private ArrayList<Contact> checkContacts() {
ContentResolver cr = getActivity().getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (cur.moveToNext())
{
Contact newContact = new Contact();
newContact.setName(getString(cur .getColumnIndex("ContactsContract.Contacts.DISPLAY_NAME")));
myContactList.add(newContact);
}
XmlContact();
return (myContactList);
}
//在这里我创建了我想放置联系人的xml文件
private void XmlContact() {
File newxmlfile = new File(Environment.getExternalStorageDirectory()+ "/ContactFile.xml");
try
{Log.v(BackupFragment.this.getClass().getName(), "create file:" + newxmlfile.createNewFile());}
catch (IOException e)
{Log.e("IOException", "exception in createNewFile() method");}
FileOutputStream fileos = null;
try
{fileos = new FileOutputStream(newxmlfile);}
catch (FileNotFoundException e)
{Log.e("FileNotFoundException", "can't create FileOutputStream");}
XmlSerializer serializer = Xml.newSerializer();
try {
serializer.setOutput(fileos, "UTF-8");
serializer.startDocument(null, Boolean.valueOf(true));
serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output",true);
serializer.startTag("", "Document");
for(int i = 0 ; i < myContactList.size(); i++) {
serializer.startTag("", "Contact");
serializer.startTag(null, "name");
serializer.text(myContactList.get(i).getName());
serializer.endTag(null, "name");
serializer.endTag("", "Contact");
}
serializer.endTag("", "Document");
serializer.endDocument();
serializer.flush();
fileos.close();
} catch (Exception e) {
Log.e("Exception", "error occurred while creating xml file");
}
}
该代码无效,我收到错误