java.lang.IllegalArgumentException:获取联系人时列数据1无效

时间:2018-04-04 08:02:48

标签: android android-contacts

这是我的代码,它显示了java.lang.IllegalArgumentException:logcat中的列data1异常无效。我的预测是否正确?我认为我的预测有些不对劲。请帮忙。提前致谢

    public  List<ContactModel> getContacts(Context ctx) {
    List<ContactModel> list = new ArrayList<>();
    String[] projection = { ContactsContract.PhoneLookup.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Phone.NUMBER,
            ContactsContract.CommonDataKinds.Phone.CONTACT_ID };
    ContentResolver cResolver=ctx.getContentResolver();
    ContentProviderClient mCProviderClient = cResolver.acquireContentProviderClient(ContactsContract.Contacts.CONTENT_URI);
    ArrayList<String> mContactList = null;
    try
    {
        Cursor mCursor = mCProviderClient.query(ContactsContract.Contacts.CONTENT_URI, projection, null, null, null);
        if (mCursor.getCount() > 0) {
        while (mCursor.moveToNext()) {
            String id = mCursor.getString(mCursor.getColumnIndex(ContactsContract.Contacts._ID));
            if (mCursor.getInt(mCursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
                Cursor cursorInfo = ctx.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);
                InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(ctx.getContentResolver(),
                        ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(id)));
                while (cursorInfo.moveToNext()) {
                    ContactModel info = new ContactModel();
                    info.id = id;

                    list.add(info);
                }

                cursorInfo.close();
            }
        }
        mCursor.close();
    }

    }
    catch (RemoteException e)
    {
        e.printStackTrace();
        mContactList = null;
    }
    catch (Exception e)
    {
        e.printStackTrace();
        mContactList = null;
    }

    return list;
    }

1 个答案:

答案 0 :(得分:0)

投影确实不正确。

Contacts DB分为三个主要表:

  1. Contacts - 每个条目代表一个联系人,并将一个或多个RawContacts
  2. 组合在一起
  3. RawContacts - 每个条目代表有关某些SyncAdapter(例如Whatsapp,Google,Facebook,Viber)同步的联系人的数据,此组合包含多个数据条目
  4. Data - 有关联系人,电子邮件,电话等的实际数据,每一行都是属于单个RawContact的单个数据
  5. 您正试图在Contacts表格上进行查询,并使用Data表格中的字段进行投影,但您无法做到这一点。

    您可以使用类似以下代码的内容,只需转换HashMap即可使用ContactModel对象:

    Map<Long, List<String>> contacts = new HashMap<Long, List<String>>();
    
    String[] projection = { Data.CONTACT_ID, Data.DISPLAY_NAME, Data.MIMETYPE, Data.DATA1, Data.DATA2, Data.DATA3 };
    String selection = Data.MIMETYPE + " IN ('" + Phone.CONTENT_ITEM_TYPE + "')";
    Cursor cur = cr.query(Data.CONTENT_URI, projection, selection, null, null);
    
    while (cur != null && cur.moveToNext()) {
        long id = cur.getLong(0);
        String name = cur.getString(1);
        String mime = cur.getString(2); // type of data (e.g. "phone")
        String data = cur.getString(3); // the actual info, e.g. +1-212-555-1234
        int type = cur.getInt(4); // a numeric value representing type: e.g. home / office / personal
        String label = cur.getString(5); // a custom label in case type is "TYPE_CUSTOM"
    
        String labelStr = Phone.getTypeLabel(getResources(), type, label);
        Log.d(TAG, "got " + id + ", " + name + ", " + kind + " - " + data + " (" + labelStr + ")");
    
        // add info to existing list if this contact-id was already found, or create a new list in case it's new
        List<String> infos;
        if (contacts.containsKey(id)) {
            infos = contacts.get(id);
        } else {
            infos = new ArrayList<String>();
            infos.add("name = " + name);
            contacts.put(id, infos);
        }
        infos.add(kind + " = " + data + " (" + labelStr + ")");
    }