Upload contacts from Android device - Contactscontract.Contacts - Contentprovider

Asked

Viewed 841 times

0

I am trying to upload the following data from the user’s calendar: name, phone, email and photo.

However the process is very slow, it is taking on average 4 minutes to load information from 800 contacts.

I searched, but unsuccessfully, for some library to do this.

Is there any other way to make this shipment?

The code I’m using:

private void carregaContatos() {
        try {
            cr = activity.getContentResolver();
            contatos = new ArrayList<ContatosUsuario>();

            Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI,
                    null, null, null, null);

            if (cursor != null) {
                if (cursor.moveToFirst()) {
                    do {
                        JSONObject contatosJson = new JSONObject();
                        ContatosUsuario contato = new ContatosUsuario();
                        // pega id do contato dentro do cursor
                        String idContato = cursor.getString(cursor
                                .getColumnIndex(ContactsContract.Contacts._ID));
                        String idLokkup = cursor
                                .getString(cursor
                                        .getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
                        contato.setId(idContato + "&" + idLokkup);
                        contato.setFavorito(null);
                        // pega nome
                        String nome = cursor.getString(cursor
                                .getColumnIndex(Contacts.DISPLAY_NAME));

                        contato.setNome(nome);
                        contatosJson.put("nome", contato.getNome());
                        // pega numeros de telefone
                        if (Integer
                                .parseInt(cursor.getString(cursor
                                        .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                            ArrayList<String> numeros = new ArrayList<String>();
                            numeros = getNumerosContato(idContato);
                            if ((numeros != null) && (numeros.size() > 0)) {
                                contatosJson.put("numeros", arrayNumero);
                                contato.setNumero(numeros.get(0));
                                contato.setTodosNumeros(numeros);
                            } else {
                                arrayNumero = new JSONArray();
                            }
                        }

                        // pega email
                        ArrayList<String> emails = new ArrayList<String>();
                        emails = getEmails(idContato);
                        if ((emails != null) && (emails.size() > 0)) {
                            contato.setEmail(emails);
                            contatosJson.put("emails", arrayEmail);
                        } else {
                            arrayEmail = new JSONArray();
                        }

                        // pega foto
                        Bitmap foto = getContactPhoto(idContato);
                        if (foto != null) {
                            contato.setImagem(Utils.encodeTobase64(foto));
                        } else {
                            contato.setImagem(null);
                        }
                        if ((contato.getTodosNumeros() != null)
                                && (contato.getTodosNumeros().size() > 0)) {
                            contatos.add(contato);
                        }
                        arrayContato.put(contatosJson);
                    } while (cursor.moveToNext());
                }
            }
        } catch (JSONException e) {
        }
    }

    private ArrayList<String> getNumerosContato(String idContato) {
        ArrayList<String> numeros = new ArrayList<String>();
        Cursor cursor = cr.query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                new String[] { idContato }, null);

        if (cursor.moveToFirst()) {
            do {
                String numero = cursor.getString(cursor
                        .getColumnIndex(Phone.NUMBER));
                numeros.add(numero);
                arrayNumero.put(numero);
            } while (cursor.moveToNext());
        }
        if (cursor != null) {
            cursor.close();
        }
        return numeros;
    }

    private ArrayList<String> getEmails(String idContato) {
        ArrayList<String> emails = new ArrayList<String>();

        Cursor cursor = cr.query(
                ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                new String[] { idContato }, null);
        if (cursor.moveToFirst()) {
            do {
                String email = cursor.getString(cursor
                        .getColumnIndex(Email.DATA));

                emails.add(email);
                arrayEmail.put(email);
            } while (cursor.moveToNext());
        }
        if (cursor != null) {
            cursor.close();
        }

        return emails;
    }

    private Bitmap getContactPhoto(String idContato) {

        Uri uri = ContentUris.withAppendedId(
                ContactsContract.Contacts.CONTENT_URI,
                Long.parseLong(idContato));

        InputStream input = ContactsContract.Contacts
                .openContactPhotoInputStream(cr, uri);

        if (input == null) {
            return null;
        }

        return BitmapFactory.decodeStream(input);
    }
  • It’s just slow reading or are you sending to some webservice?

  • It is in a webservice that I call the class that has the code I sent

  • It probably should be taking time to communicate with the webservice so, taking into account that are 800 contacts.

1 answer

1


I answered a question similar to this one...

This link will help you clone the contact list of your mobile device (Android)

These links -> (Part 1 and Part 2) found complete, Voce can read that will help you a lot..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.