performance - Android fetching phone number, name and photo from contacts taking too much time -
this code fetching contacts, name , photo contact list taking time in fetching these.
so problem if user has more 500 contacts taking time how decreasing fetching time.
i have seen many posts on stack overflow unable find solution.
public void fetchcontacts() { list = new arraylist<persondetails>(); string phonenumber = null; //uri content_uri = contactscontract.contacts.content_uri; uri content_uri= contactscontract.commondatakinds.phone.content_uri; string _id = contactscontract.contacts._id; string display_name = contactscontract.contacts.display_name; string has_phone_number = contactscontract.contacts.has_phone_number; uri phonecontent_uri = contactscontract.commondatakinds.phone.content_uri; string phone_contact_id = contactscontract.commondatakinds.phone.contact_id; string number = contactscontract.commondatakinds.phone.number; // stringbuffer output = new stringbuffer(); string[] projection = new string[] { contactscontract.contacts._id, contactscontract.contacts.display_name, contactscontract.contacts.has_phone_number, }; string selection = contactscontract.contacts.has_phone_number + "='1'"; contentresolver contentresolver = getcontentresolver(); cursor cursor = contentresolver.query(content_uri, projection, selection, null, null); // loop every contact in phone if (cursor.getcount() > 0) { while (cursor.movetonext()) { //log.e("i", + ""); persondetails persondetails = null; string contact_id = cursor.getstring(cursor .getcolumnindex(_id)); string name = cursor.getstring(cursor .getcolumnindex(display_name)); int hasphonenumber = integer.parseint(cursor .getstring(cursor .getcolumnindex(has_phone_number))); if (hasphonenumber > 0) { // query , loop every phone number of // contact string phone_projection[] = new string[] { phone.number }; cursor phonecursor = contentresolver.query( phonecontent_uri, phone_projection, phone_contact_id + " = ?", new string[] { contact_id }, null); while (phonecursor.movetonext()) { phonenumber = phonecursor.getstring(phonecursor .getcolumnindex(number)); // output.append("\n phone number:" + // phonenumber); } phonecursor.close(); if (phonenumber != null && phonenumber.length() >= 10) { uri ur = null; try { uri person = contenturis .withappendedid( contactscontract.contacts.content_uri, long.parselong(contact_id)); ur = uri.withappendedpath( person, contactscontract.contacts.photo.content_directory); } catch (numberformatexception e) { // todo auto-generated catch block e.printstacktrace(); } persondetails = new persondetails(); if (name != null) { persondetails.setname(name); } if (phonenumber != null) { //log.e("phone", phonenumber); persondetails.setsocialid(phonenumber); } if (ur != null) { //log.e("uri", ur.tostring()); persondetails.setphotourl(ur.tostring()); } persondetails.setsocialflag(5); } } if (persondetails != null) { list.add(persondetails); } } } try { if (cursor != null) { cursor.close(); } } catch (exception e) { e.printstacktrace(); } }
Comments
Post a Comment