private static InputStream getContactPhotoStream(Context context, Uri uri) {
   if (VERSION.SDK_INT >= VERSION_CODES.ICE_CREAM_SANDWICH) {
     return ContactsContract.Contacts.openContactPhotoInputStream(
         context.getContentResolver(), uri, true);
   } else {
     return ContactsContract.Contacts.openContactPhotoInputStream(
         context.getContentResolver(), uri);
   }
 }
  // 根据号码获得联系人头像
  public Bitmap getPeopleImage(String x_number) {

    // 获得Uri
    Uri uriNumber2Contacts =
        Uri.parse("content://com.android.contacts/" + "data/phones/filter/" + x_number);
    // 查询Uri,返回数据集
    Cursor cursorCantacts =
        mContext.getContentResolver().query(uriNumber2Contacts, null, null, null, null);
    // 如果该联系人存在
    if (cursorCantacts.getCount() > 0) {
      // 移动到第一条数据
      cursorCantacts.moveToFirst();
      // 获得该联系人的contact_id
      Long contactID = cursorCantacts.getLong(cursorCantacts.getColumnIndex("contact_id"));
      cursorCantacts.close();
      // 获得contact_id的Uri
      Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactID);
      // 打开头像图片的InputStream
      InputStream input =
          ContactsContract.Contacts.openContactPhotoInputStream(mContext.getContentResolver(), uri);
      // 从InputStream获得bitmap
      return BitmapFactory.decodeStream(input);
    }
    cursorCantacts.close();
    return null;
  }
  public void getImage(String strPhoneNumber) {
    // 取得Intent中的頭像

    // 通话电话号码获取头像uri
    Uri uriNumber2Contacts =
        Uri.parse("content://com.android.contacts/" + "data/phones/filter/" + strPhoneNumber);
    Cursor cursorCantacts =
        ContactActivity.this.getContentResolver().query(uriNumber2Contacts, null, null, null, null);
    if (cursorCantacts.getCount() > 0) { // 若游标不为0则说明有头像,游标指向第一条记录
      cursorCantacts.moveToFirst();
      Long contactID = cursorCantacts.getLong(cursorCantacts.getColumnIndex("contact_id"));
      Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactID);
      InputStream input =
          ContactsContract.Contacts.openContactPhotoInputStream(
              ContactActivity.this.getContentResolver(), uri);
      if (input == null) {
        Bitmap defaultbitmap =
            BitmapFactory.decodeResource(
                ContactActivity.this.getResources(), R.drawable.defaultcontact);
        bitmapList.add(defaultbitmap);
      } else {
        Bitmap btContactImage = BitmapFactory.decodeStream(input);
        bitmapList.add(btContactImage);
      }
    }
  }
Beispiel #4
0
 private Uri lookupContact(String phone) {
   Cursor c = null;
   try {
     Uri phoneUri =
         Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phone));
     String[] projection =
         new String[] {ContactsContract.Contacts._ID, ContactsContract.Contacts.LOOKUP_KEY};
     c = mContext.getContentResolver().query(phoneUri, projection, null, null, null);
     if (c != null && c.getCount() > 0) {
       c.moveToFirst();
       int lookupIdx = c.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY);
       int idIdx = c.getColumnIndex(ContactsContract.Contacts._ID);
       String lookupKey = c.getString(lookupIdx);
       long contactId = c.getLong(idIdx);
       return ContactsContract.Contacts.getLookupUri(contactId, lookupKey);
     }
   } catch (Throwable t) {
     Log.w(TAG, "Problem getting content resolver or performing contacts query.", t);
   } finally {
     if (c != null) {
       c.close();
     }
   }
   return null;
 }
Beispiel #5
0
  public static ArrayList<ContactMessage> getContacts(Context context) {
    ArrayList<ContactMessage> contactList = new ArrayList<ContactMessage>();

    Cursor cursor =
        context
            .getContentResolver()
            .query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    if (cursor.moveToFirst()) {
      while (cursor.moveToNext()) {
        String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
        String name =
            cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

        byte[] imageData = null;
        Uri photoUri =
            ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.parseLong(id));
        InputStream input =
            ContactsContract.Contacts.openContactPhotoInputStream(
                context.getContentResolver(), photoUri);
        if (input != null) {
          ByteArrayOutputStream buffer = new ByteArrayOutputStream();
          int nRead;
          byte[] data = new byte[16384];
          try {
            while ((nRead = input.read(data, 0, data.length)) != -1) {
              buffer.write(data, 0, nRead);
            }
            buffer.flush();
            imageData = buffer.toByteArray();
          } catch (IOException e) {
            e.printStackTrace();
          }
        }

        if (Integer.parseInt(
                cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)))
            > 0) {
          Cursor pCur =
              context
                  .getContentResolver()
                  .query(
                      ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                      null,
                      ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                      new String[] {id},
                      null);
          if (pCur.moveToNext()) {
            String contactNumber =
                pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            contactList.add(new ContactMessage(contactNumber, name, imageData));
          }
          pCur.close();
        }
      }
    }
    cursor.close();
    Log.w("Hermes", Integer.toString(contactList.size()));
    return contactList;
  }
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		super.onClick(v);
		
		switch(v.getId()) {
		case R.id.bt_contact:
			Uri uri = ContactsContract.Contacts.getLookupUri(((ViewHolder)findViewHolder(v))
					.contact.getContactId(), (((ViewHolder)findViewHolder(v)).contact.getLookUpKey()));
		    QuickContact.showQuickContact(this, v, uri, QuickContact.MODE_LARGE, null);
			break;
		}
		
		
	}
Beispiel #7
0
  @Override
  @TargetApi(21)
  protected Drawable doInBackground(Void... voids) {
    // contact photo
    Uri contactUri = null;
    if (contactId > 0) {
      final Cursor cursorWithContact =
          context
              .getContentResolver()
              .query(
                  Uri.withAppendedPath(SPOCContentProvider.CONTACTS_URI, String.valueOf(contactId)),
                  new String[] {Contact.COLUMN_CONTACT_KEY},
                  null,
                  null,
                  null);
      if (cursorWithContact.moveToFirst()) {
        contactUri =
            Uri.withAppendedPath(
                ContactsContract.Contacts.CONTENT_LOOKUP_URI, cursorWithContact.getString(0));
      }
      cursorWithContact.close();
    } else if (lookupKey != null) {
      contactUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
    }

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

    final InputStream inputStream =
        ContactsContract.Contacts.openContactPhotoInputStream(
            context.getContentResolver(), contactUri);

    RoundedBitmapDrawable roundedBitmapDrawable;
    if (inputStream != null) {
      roundedBitmapDrawable =
          RoundedBitmapDrawableFactory.create(context.getResources(), inputStream);
      roundedBitmapDrawable.setCircular(true);
      return roundedBitmapDrawable;
    }

    if (Build.VERSION.SDK_INT >= 21) {
      return context.getResources().getDrawable(R.drawable.user, context.getTheme());
    } else {
      //noinspection deprecation
      return context.getResources().getDrawable(R.drawable.user);
    }
  }
  public Bitmap getBitmap(Context context) {
    Bitmap bitmap = null;
    ContentResolver contentResolver = context.getContentResolver();

    try {
      Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId);
      InputStream input =
          ContactsContract.Contacts.openContactPhotoInputStream(contentResolver, uri);
      if (input != null) {
        bitmap = BitmapFactory.decodeStream(input);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }

    return bitmap;
  }
  @Override
  public Bitmap getContactPhoto(Context ctxt, Uri uri, boolean hiRes, Integer defaultResource) {
    Bitmap img = null;
    InputStream s =
        ContactsContract.Contacts.openContactPhotoInputStream(
            ctxt.getContentResolver(), uri, hiRes);
    img = BitmapFactory.decodeStream(s);

    if (img == null && defaultResource != null) {
      BitmapDrawable drawableBitmap =
          ((BitmapDrawable) ctxt.getResources().getDrawable(defaultResource));
      if (drawableBitmap != null) {
        img = drawableBitmap.getBitmap();
      }
    }
    return img;
  }
  public Bitmap loadBitmap(Context context) {
    // L.d("contact", "contact+id:" + contactid);
    if (contactId <= 0) return null;

    Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId);
    InputStream input =
        ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(), uri);
    Bitmap bitmap = null;
    if (input != null) {

      bitmap = BitmapFactory.decodeStream(input);
    }
    try {
      input.close();
    } catch (Throwable t) {
    }
    return bitmap;
  }
Beispiel #11
0
  /**
   * loads the profile picture of the main ("me") contact
   * http://developer.android.com/reference/android/provider/ContactsContract.Profile.html
   *
   * @param contentResolver
   * @param highRes true for large image if present, false for thumbnail
   * @return bitmap of loaded photo
   */
  public static Bitmap loadMainProfilePhoto(ContentResolver contentResolver, boolean highRes) {
    try {
      long mainProfileContactId = getMainProfileContactId(contentResolver);

      Uri contactUri =
          Uri.withAppendedPath(
              ContactsContract.Contacts.CONTENT_URI, Long.toString(mainProfileContactId));
      InputStream photoInputStream =
          ContactsContract.Contacts.openContactPhotoInputStream(
              contentResolver, contactUri, highRes);
      if (photoInputStream == null) {
        return null;
      }
      return BitmapFactory.decodeStream(photoInputStream);
    } catch (Throwable ignored) {
      return null;
    }
  }
Beispiel #12
0
  public static Bitmap loadPhotoByContactId(
      ContentResolver contentResolver, long contactId, boolean highRes) {
    if (contactId == -1) {
      return null;
    }
    Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId);
    // older android versions (tested on API level 15) fail on lookupuris being passed to
    // openContactPhotoInputStream
    // http://stackoverflow.com/a/21214524/3000919
    // Uri lookupUri = ContactsContract.Contacts.getLookupUri(contentResolver, contactUri);
    // Also, we don't need a permanent shortcut to the contact since we load it afresh each time

    InputStream photoInputStream =
        ContactsContract.Contacts.openContactPhotoInputStream(contentResolver, contactUri, highRes);

    if (photoInputStream == null) {
      return null;
    }
    return BitmapFactory.decodeStream(photoInputStream);
  }
Beispiel #13
0
  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    // Get the Cursor
    Cursor cursor = ((SimpleCursorAdapter) parent.getAdapter()).getCursor();
    // Move to the selected contact
    cursor.moveToPosition(position);
    // Get the _ID value
    mContactId = cursor.getLong(CONTACT_ID_INDEX);
    // Get the selected LOOKUP KEY
    mContactKey = cursor.getString(LOOKUP_KEY_INDEX);
    // Create the contact's content Uri
    mContactUri = ContactsContract.Contacts.getLookupUri(mContactId, mContactKey);
    /*
     * You can use mContactUri as the content URI for retrieving
     * the details for a contact.
     */
    if (ACTION.equals(SHOW_CONTACT_ACTION)) {
      Intent openContact = new Intent(Intent.ACTION_VIEW, mContactUri);
      startActivity(openContact);
    } else {

    }
  }
 private InputStream getInputStream(Request data) throws IOException {
   ContentResolver contentResolver = context.getContentResolver();
   Uri uri = data.uri;
   switch (matcher.match(uri)) {
     case ID_LOOKUP:
       uri = ContactsContract.Contacts.lookupContact(contentResolver, uri);
       if (uri == null) {
         return null;
       }
       // Resolved the uri to a contact uri, intentionally fall through to process the resolved uri
     case ID_CONTACT:
       if (SDK_INT < ICE_CREAM_SANDWICH) {
         return openContactPhotoInputStream(contentResolver, uri);
       } else {
         return ContactPhotoStreamIcs.get(contentResolver, uri);
       }
     case ID_THUMBNAIL:
     case ID_DISPLAY_PHOTO:
       return contentResolver.openInputStream(uri);
     default:
       throw new IllegalStateException("Invalid uri: " + uri);
   }
 }
  public Drawable getContactDrawable(Uri contactUri) {
    long contactId = -1;
    // Load the display name for the specified person
    Cursor cursor = resolver.query(contactUri, new String[] {Contacts._ID}, null, null, null);
    try {
      if (cursor.moveToFirst()) {
        contactId = cursor.getLong(0);
      }
    } finally {
      cursor.close();
    }

    // TODO: do this better (default image and some not working?)
    // those which come from facebook do not work.. some way to get those?
    // see contacts app :)
    Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId);
    InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(resolver, uri);
    if (input == null) {
      return context.getResources().getDrawable(R.drawable.vcard_default);
    } else {
      return new BitmapDrawable(BitmapFactory.decodeStream(input));
    }
  }
Beispiel #16
0
 private boolean isEnterpriseContactId(String contactId) {
   return ContactsContract.Contacts.isEnterpriseContactId(Long.valueOf(contactId));
 }