Upload contacts from Android device - ContactsContract.Contacts - ContentProvider

0

I'm trying to load the following data from the user's calendar: name, phone, email, and photo.

Though the process is very slow, it takes an average of 4 minutes to load 800 contact information.

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

Is there any other way to do this loading?

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);
    }
    
asked by anonymous 10.11.2014 / 16:33

1 answer

1

I answered a question looks like this one .. .

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

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

    
10.11.2014 / 18:18