Exemple #1
0
  @SuppressLint("InlinedApi")
  public static ArrayList<Message_Thread> allMessage_Thread(Context context) {
    ArrayList<Integer> has = new ArrayList<Integer>();
    ArrayList<Message_Thread> list = new ArrayList<Message_Thread>();
    String[] projection = new String[] {Sms.THREAD_ID, Sms.DATE, Sms.BODY, Sms.READ, Sms.ADDRESS};
    String selection = null;
    String[] selectionArgs = null;
    String sortOrder = Sms.DATE + " desc";
    Cursor cursor =
        context
            .getContentResolver()
            .query(sms_uri, projection, selection, selectionArgs, sortOrder);
    while (cursor.moveToNext()) {
      int thread_id = cursor.getInt(cursor.getColumnIndex(Sms.THREAD_ID));
      if (has.contains(thread_id)) {
        continue;
      }
      has.add(thread_id);
      long thread_date = cursor.getLong(cursor.getColumnIndex(Sms.DATE));
      String thread_snippet = cursor.getString(cursor.getColumnIndex(Sms.BODY));
      int thread_read = cursor.getInt(cursor.getColumnIndex(Sms.READ));
      String phone = cursor.getString(cursor.getColumnIndex(Sms.ADDRESS));
      String name = "";
      byte[] photo = null;
      if (phone != null) {
        phone = phone.replace("+86", "");
      }

      // <------------ 通过phone查找头像、姓名 --------------->
      Uri uriNumber2Contacts =
          Uri.parse("content://com.android.contacts/data/phones/filter/" + phone);
      Cursor cursorCantacts =
          context.getContentResolver().query(uriNumber2Contacts, null, null, null, null);
      if (cursorCantacts.getCount() > 0) { // 若游标不为0则说明有头像,游标指向第一条记录
        cursorCantacts.moveToFirst();
        Long contactID = cursorCantacts.getLong(cursorCantacts.getColumnIndex("contact_id"));
        name = cursorCantacts.getString(cursorCantacts.getColumnIndex(Contacts.DISPLAY_NAME));
        Uri uri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactID);
        InputStream input = Contacts.openContactPhotoInputStream(context.getContentResolver(), uri);
        if (input != null) {
          try {
            photo = toByteArray(input);
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
      }

      Message_Thread thread =
          new Message_Thread(
              thread_id, thread_date, 0, thread_snippet, thread_read, name, phone, photo);
      list.add(thread);
      cursorCantacts.close();
    }

    cursor.close();
    return list;
  }
Exemple #2
0
    /*
     * Load the avatar data from the cursor into memory.  Don't decode the data
     * until someone calls for it (see getAvatar).  Hang onto the raw data so that
     * we can compare it when the data is reloaded.
     * TODO: consider comparing a checksum so that we don't have to hang onto
     * the raw bytes after the image is decoded.
     */
    private byte[] loadAvatarData(Contact entry) {
      byte[] data = null;

      if ((!entry.mIsMe && entry.mPersonId == 0) || entry.mAvatar != null) {
        return null;
      }

      if (Log.isLoggable(LogTag.CONTACT, Log.DEBUG)) {
        log("loadAvatarData: name=" + entry.mName + ", number=" + entry.mNumber);
      }

      // If the contact is "me", then use my local profile photo. Otherwise, build a
      // uri to get the avatar of the contact.
      Uri contactUri =
          entry.mIsMe
              ? Profile.CONTENT_URI
              : ContentUris.withAppendedId(Contacts.CONTENT_URI, entry.mPersonId);

      InputStream avatarDataStream =
          Contacts.openContactPhotoInputStream(mContext.getContentResolver(), contactUri);
      try {
        if (avatarDataStream != null) {
          data = new byte[avatarDataStream.available()];
          avatarDataStream.read(data, 0, data.length);
        }
      } catch (IOException ex) {
        //
      } finally {
        try {
          if (avatarDataStream != null) {
            avatarDataStream.close();
          }
        } catch (IOException e) {
        }
      }

      return data;
    }
  public Bitmap getSystemAvatarViaSystemContactId(long systemContactId) {
    if (systemContactId <= 0) {
      NmsLog.error(TAG, "getSystemAvatarViaSystemContactId. sysContactId is invalid!");
      return null;
    }

    Bitmap result = null;
    Cursor cursor = null;
    try {
      cursor =
          NmsContentResolver.query(
              mContext.getContentResolver(),
              Contacts.CONTENT_URI,
              new String[] {Contacts.PHOTO_ID},
              Contacts._ID + "=?",
              new String[] {String.valueOf(systemContactId)},
              null);
      if (cursor != null && cursor.moveToFirst()) {
        long photoId = cursor.getLong(cursor.getColumnIndex(Contacts.PHOTO_ID));
        if (photoId > 0) {
          Uri uri = ContentUris.withAppendedId(Contacts.CONTENT_URI, systemContactId);
          InputStream input =
              Contacts.openContactPhotoInputStream(mContext.getContentResolver(), uri);
          result = BitmapFactory.decodeStream(input);
        }
      }
    } catch (Exception e) {
      NmsLog.nmsPrintStackTrace(e);
    } finally {
      if (cursor != null) {
        cursor.close();
        cursor = null;
      }
    }

    return result;
  }