Export Android Contacts to XML File? -
as little school project, i'm trying create simple export application android. @ first, want exported contact list - , import device. have 2 devices testing - 1 running 2.2.3 , running 4.0.4.
i've been playing around following pieces of code accomplish getting contacts information, , saving xml file external storage:
package com.example.contacts; import java.util.arraylist; import android.os.bundle; import android.provider.contactscontract; import android.app.activity; import android.content.contentresolver; import android.database.cursor; import android.util.log; import android.view.menu; public class mainactivity extends activity { string num = "nope"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); getcontacts(); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } public string getcontacts() { 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)); string con = cur.getstring(cur.getcolumnindex(contactscontract.contacts.display_name)); if (integer.parseint(cur.getstring( cur.getcolumnindex(contactscontract.contacts.has_phone_number))) > 0) { cursor pcur = cr.query( contactscontract.commondatakinds.phone.content_uri, null, contactscontract.commondatakinds.phone.contact_id +" = ?", new string[]{id}, null); while (pcur.movetonext()) { string num = pcur.getstring(pcur.getcolumnindex(contactscontract.commondatakinds.phone.number)); log.d("contacts: ", "name: " + con + ", number: " + num); return num; } pcur.close(); } } } return num; } } this code prints contacts log.
the next code creates xml file @ device's external storage (sd card) - taken http://www.anddev.org/write_a_simple_xml_file_in_the_sd_card_using_xmlserializer-t8350.html:
package com.example.contacts; import java.io.file; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; import org.xmlpull.v1.xmlserializer; import android.app.activity; import android.os.bundle; import android.os.environment; import android.util.log; import android.util.xml; import android.widget.textview; public class xmlfilecreator extends activity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); //create new file called "new.xml" in sd card file newxmlfile = new file(environment.getexternalstoragedirectory()+"/new.xml"); try{ newxmlfile.createnewfile(); }catch(ioexception e){ log.e("ioexception", "exception in createnewfile() method"); } //we have bind new file fileoutputstream fileoutputstream fileos = null; try{ fileos = new fileoutputstream(newxmlfile); }catch(filenotfoundexception e){ log.e("filenotfoundexception", "can't create fileoutputstream"); } //we create xmlserializer in order write xml data xmlserializer serializer = xml.newserializer(); try { //we set fileoutputstream output serializer, using utf-8 encoding serializer.setoutput(fileos, "utf-8"); //write <?xml declaration encoding (if encoding not null) , standalone flag (if standalone not null) serializer.startdocument(null, boolean.valueof(true)); //set indentation option serializer.setfeature("http://xmlpull.org/v1/doc/features.html#indent- output", true); //start tag called "root" serializer.starttag(null, "root"); //i indent code have view similar xml-tree serializer.starttag(null, "child1"); serializer.endtag(null, "child1"); serializer.starttag(null, "child2"); //set attribute called "attribute" "value" <child2> serializer.attribute(null, "attribute", "value"); serializer.endtag(null, "child2"); serializer.starttag(null, "child3"); //write text inside <child3> serializer.text("some text inside child3"); serializer.endtag(null, "child3"); serializer.endtag(null, "root"); serializer.enddocument(); //write xml data fileoutputstream serializer.flush(); //finally close file stream fileos.close(); textview tv = (textview)this.findviewbyid(r.id.result); tv.settext("file has been created on sd card"); } catch (exception e) { log.e("exception","error occurred while creating xml file"); } } } now, question follows: how 1 proceed add strings of contacts xml file? guess have add tags this:
starttag: contacts starttag: name :endtag starttag: number :endtag : endtag
but how can write these contact details child tags of first "contacts" tag? reference lovely.
i make life easier , not write xml 'string' myself use serializer simple, have @ examples , reference, it's easy use.
Comments
Post a Comment