获取用户的Google帐户个人资料图片

时间:2015-04-25 20:39:42

标签: java android inputstream profile

到目前为止,我已经看过许多问题,这是主题,到目前为止,没有一个答案对我有用。在我的代码中,我获得了用户的Google帐户及其联系信息。我可以成功查看他们的电子邮件并显示名称。当我抓住他们的id号并使用http://developer.android.com/reference/android/provider/ContactsContract.Contacts.Photo.html让我使用它时,InputStream会出现null。当我通过以下方法调试时,程序永远不会将blob加载到数据中,它会立即跳到最后。

public InputStream openPhoto(long contactId) {
        Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId);
        Uri photoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
        Cursor cursor = context.getContentResolver().query(photoUri,
                new String[] {ContactsContract.Contacts.Photo.PHOTO}, null, null, null);
        if (cursor == null) {
            return null;
        }
        try {
            if (cursor.moveToFirst()) {
                byte[] data = cursor.getBlob(0);
                if (data != null) {
                    return new ByteArrayInputStream(data);
                }
            }
        } finally {
            cursor.close();
        }
        return null;
    }

这是否意味着所选联系人没有个人资料图片???我不是这样的,因为它正在访问我的个人Gmail帐户并正确地提供其他数据。我使用以下代码(以及电子邮件和显示名称)获取此ID。我收到了googleAccount.name

的电子邮件
    AccountManager accountManager;
    accountManager = AccountManager.get(context);

    Account[] account = accountManager.getAccounts();
    Account googleAccount = null;
    for(int i = 0; i < account.length; i++) {
        if(account[i].type.equals("com.google")) {
            googleAccount = account[i];
        }
    }
    Cursor c = context.getContentResolver().query(ContactsContract.Profile.CONTENT_URI, null, null, null, null);
    int count = c.getCount();
    c.moveToFirst();
    String id = "";
    String displayName = "";
    String[] columnNames = c.getColumnNames();
    int position = c.getPosition();
    if (count == 1 && position == 0) {
        for (int j = 0; j < columnNames.length; j++) {
            String columnName = columnNames[j];
            String columnValue = c.getString(c.getColumnIndex(columnName));
            if(columnName.equals("_id"))
                id = columnValue;
            if(columnName.equals("display_name"))
                displayName = columnValue;
            // consume the values here
        }
    }
    c.close();

为什么此输入流会出现空值?我直接从谷歌获得了代码。以前有没有人对此代码有这个问题?

0 个答案:

没有答案