获取主要帐户照片

时间:2014-07-24 19:55:38

标签: android contacts google-account

我正在创建一个应用程序,我为用户提供了一张照片,我想自动将其拉出来。我不确定如何做到这一点或者甚至是最好的方法。

有没有办法拉谷歌帐户照片?考虑到全世界很多人可能根本没有谷歌帐户,这可能不是最好的方式。

ContactProvider中是否有手机用户的默认联系人?如果是,我将如何获取它及其拥有的照片。对不起,我没有任何代码,我只是不知道从哪里开始,建议或链接可能会指出我正确的方向。

我试过的代码

String[] mProjection = new String[]
            {
            Profile._ID,
            Profile.DISPLAY_NAME_PRIMARY,
            Profile.LOOKUP_KEY,
            Profile.PHOTO_THUMBNAIL_URI
            };

    // Retrieves the profile from the Contacts Provider
    Cursor mProfileCursor = mContext.getContentResolver().query(
            Profile.CONTENT_URI,
            mProjection ,
            null,
            null,
            null);

    try {
         if (mProfileCursor.moveToFirst()) {
             byte[] data = mProfileCursor.getBlob(0); //error on this line
             if (data != null) {
                 InputStream is = new ByteArrayInputStream(data);
                 imageBit = BitmapFactory.decodeStream(is);
             }
         }
     } finally {
         mProfileCursor.close();
     }

我一直在注意到的行上出错:

android.database.sqlite.SQLiteException:未知错误(代码0):nativeGetBlob中的INTEGER数据

编辑2:所以我将0更改为3以获取配置文件照片它修复了我的一个错误但现在我从解码流获得的imageBit为空。然而,字节[]并非如此,我不知道将byte []转换为位图

的问题是什么

1 个答案:

答案 0 :(得分:1)

ContactsContract.Contacts内,您可以找到专用于手机所有者的行。

请务必向您的应用添加READ_PROFILE权限。

// Sets the columns to retrieve for the user profile
mProjection = new String[]
    {
        Profile.PHOTO_THUMBNAIL_URI
    };

// Retrieves the profile from the Contacts Provider
mProfileCursor =
        getContentResolver().query(
                Profile.CONTENT_URI,
                mProjection ,
                null,
                null,
                null);

然后使用mProfileCursor获取数据并显示它。

更多信息,here

相关问题