4

私はAndroid4で動作するこのバージョンを持っています(String emailGmailアドレスです):

private Uri getPhotoUriFromEmail(String email) {
    Uri u = null;
    String[] projection = { ContactsContract.CommonDataKinds.Email.PHOTO_URI };
    String photoUri;
    ContentResolver cr = context.getContentResolver();
    Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, projection,
            ContactsContract.CommonDataKinds.Email.ADDRESS + " = ?", 
            new String[]{email}, null);
    if (emailCur.moveToNext()) { 
        photoUri = emailCur.getString(
                emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.PHOTO_URI));
        u = Uri.parse(photoUri);
    }
    return u;
}

その2.3.6バージョンは何ですか?(また、API8を最小限のAPIサポートとして設定したときにAndroidSDKがAPI11の使用について警告するべきではありませんか?そうではなかったためです...)

4

1 に答える 1

5

ContactsContract.CommonDataKinds.Email.PHOTO_URI この方法を使用せずに連絡先の写真の uri を取得できます。

private Uri getPhotoUriFromEmail(String email) {
    Uri u = null;
    String[] projection = { ContactsContract.CommonDataKinds.Photo.CONTACT_ID };
    String photoUri;
    ContentResolver cr = getContentResolver();
    Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, 
            projection,
            ContactsContract.CommonDataKinds.Email.ADDRESS + " = ?",
            new String[]{email}, null);
    if (emailCur.moveToNext()) {
        int columnIndex = emailCur.getColumnIndex(
                              ContactsContract.CommonDataKinds.Photo.CONTACT_ID);
        long contactId = emailCur.getLong(columnIndex);

        u = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, 
                                       contactId);
        u = Uri.withAppendedPath(u, 
                                ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
    }
    return u;
}

または、次の方法でフォト ストリームを利用する方法を取得できますContactsContract.Contacts .openContactPhotoInputStream(ContentResolver, Uri)

private InputStream getPhotoInputStreamFromEmail(String email) {
    Uri u = null;
    String[] projection = { ContactsContract.CommonDataKinds.Photo.CONTACT_ID };
    String photoUri;
    ContentResolver cr = getContentResolver();
    Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
            projection,
            ContactsContract.CommonDataKinds.Email.ADDRESS + " = ?",
            new String[]{email}, null);
    if (emailCur.moveToNext()) {
        int columnIndex = emailCur.getColumnIndex(
                ContactsContract.CommonDataKinds.Photo.CONTACT_ID);
        long contactId = emailCur.getLong(columnIndex);

        u = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,
                contactId);
        return ContactsContract.Contacts.openContactPhotoInputStream(cr, u);
    }
    return null;
}
于 2012-12-31T15:21:49.240 に答える