从phoneBook我如何在Android应用程序中获取联系号码和照片

时间:2011-04-22 05:54:20

标签: android

我要打电话簿,我可以访问android的联系表格

2 个答案:

答案 0 :(得分:5)

//访问联系人照片

public static Bitmap loadContactPhoto(ContentResolver cr, long  id) {
        Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
        InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
        if (input == null) {
            return null;
        }
        return BitmapFactory.decodeStream(input);
}

//将方法调用为

Bitmap bitmap = loadContactPhoto(getContentResolver(), _id);
imageView.setImageBitmap(bitmap);

//获取数字

private void getAllNumbers(long id) {
        //Getting numbers
        Cursor phones = null;
        try {
            phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    new String[]{Phone.NUMBER, Phone.TYPE},
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id, 
                    null,
                    null);
            if(phones != null) {
                while(phones.moveToNext()){
                    switch(phones.getInt(phones.getColumnIndex(Phone.TYPE))){
                        case Phone.TYPE_MOBILE :
                            mobilePhone = phones.getString(phones.getColumnIndex(Phone.NUMBER));
                            break;
                        case Phone.TYPE_HOME :
                            homePhone = phones.getString(phones.getColumnIndex(Phone.NUMBER));
                            break;
                        case Phone.TYPE_WORK :
                            workPhone = phones.getString(phones.getColumnIndex(Phone.NUMBER));
                            break;
                        case Phone.TYPE_OTHER : // You can store other number also
                    }
                }
            }
        } catch (Exception e) {
            //Handle exception
        } finally {
            if(!phones.isClosed() || phones != null)
                phones.close();
        }
    }

快乐的编码。

答案 1 :(得分:3)

我在这里分享完整的代码。你必须改变一点,你可以从电话簿中获取姓名,图像和电话号码。 感谢

 Cursor cursor = null;
            String name, phoneNumber, image, email;
            try {
                cursor = getApplicationContext().getContentResolver()
                        .query(Phone.CONTENT_URI, null, null, null, null);
                int nameIdx = cursor.getColumnIndex(Phone.DISPLAY_NAME);
                int phoneNumberIdx = cursor.getColumnIndex(Phone.NUMBER);
                int photoIdIdx = cursor.getColumnIndex(Phone.PHOTO_URI);
                // int emailIDx
                // =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);

                cursor.moveToFirst();
                do {
                    HashMap<String, String> hashMap = new HashMap<String, String>();
                    name = cursor.getString(nameIdx);
                    phoneNumber = cursor.getString(phoneNumberIdx);
                    image = cursor.getString(photoIdIdx);
                    // email=cursor.getString(emailIDx);

                    if (!phoneNumber.contains("*")) {
                        hashMap.put("name", "" + name);
                        hashMap.put("phoneNumber", "" + phoneNumber);
                        hashMap.put("image", "" + image);
                        // hashMap.put("email", ""+email);
                        hashMapsArrayList.add(hashMap);
                    }

                } while (cursor.moveToNext());
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (cursor != null) {
                    cursor.close();
                }
            }
            myAdapter = new MyAdapter();
            listView.setAdapter(myAdapter);
            myAdapter.notifyDataSetChanged();

        }

    });
}

private class MyAdapter extends BaseAdapter

{

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return hashMapsArrayList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return hashMapsArrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder viewHolder;
        convertView = getLayoutInflater().inflate(R.layout.itemlist, null);
        viewHolder = new ViewHolder();

        if (convertView != null) {
            viewHolder.textView_Name = (TextView) convertView
                    .findViewById(R.id.textView_name);
            viewHolder.textView_Number = (TextView) convertView
                    .findViewById(R.id.textView_number);
            viewHolder.imageView = (ImageView) convertView
                    .findViewById(R.id.imageView_user);
            // viewHolder.textView_Email=(TextView)
            // convertView.findViewById(R.id.textView_email);

            viewHolder.textView_Name.setText(hashMapsArrayList
                    .get(position).get("name"));
            viewHolder.textView_Number.setText(hashMapsArrayList.get(
                    position).get("phoneNumber"));
            // viewHolder.textView_Email.setText(hashMapsArrayList.get(position).get("email"));

            String string_imageCheck = hashMapsArrayList.get(position).get(
                    "image");
            if (string_imageCheck.equalsIgnoreCase("null")) {
                viewHolder.imageView
                        .setImageResource(R.drawable.ic_launcher);

            } else {
                viewHolder.imageView
                        .setImageURI(Uri.parse(hashMapsArrayList.get(
                                position).get("image")));
            }

        }

        return convertView;
    }

    class ViewHolder {
        // TextView textView_Email;
        TextView textView_Name;
        TextView textView_Number;

        ImageView imageView;
    }