获取用户谷歌个人资料图片的完整网址



我在Android应用程序中使用Google登录,并在数据库中保存指向其个人资料图片URL的链接。

过去,我会得到完整的URL,从其中一个开始,例如:

https://lh3.googleusercontent.com/{random stuff}/photo.jpg
https://lh4.googleusercontent.com/{random stuff}/photo.jpg
https://lh5.googleusercontent.com/{random stuff}/photo.jpg
https://lh6.googleusercontent.com/{random stuff}/photo.jpg

自从谷歌播放服务更新了这个(我想在 8.3 中?我只得到

/{random stuff}/photo.jpg

这显然不会链接到图片。

这是我的代码:

    GoogleSignInAccount acct = result.getSignInAccount();
    if (acct != null) 
    {
        String profilePicPath = "";
        if (acct.getPhotoUrl() != null) {
            profilePicPath = acct.getPhotoUrl().getPath();
        }
    }

我做错了什么?

编辑:我相信我做错的是我在URL后面添加了getPath()

问题是这样的:

我在这里添加了getPath() profilePicPath = acct.getPhotoUrl().getPath();. 相反,我删除了它,得到了它的字符串形式,这就是我所需要的。

您将需要在 Google 控制台上启用 G+ API。

在此处输入链接说明

您将需要初始化GoogleApiClient。

在此处输入链接说明

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
   ......
    googleApiClient = new GoogleApiClient.Builder(getActivity())
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Plus.API)
            .addScope(Plus.SCOPE_PLUS_LOGIN)
            .addScope(Plus.SCOPE_PLUS_PROFILE)
            .build();
}
@Override
public void onConnected(Bundle bundle) {
    Plus.PeopleApi.loadVisible(googleApiClient, null).setResultCallback(this);

    if (Plus.PeopleApi.getCurrentPerson(googleApiClient) != null) {
        Person person = Plus.PeopleApi.getCurrentPerson(googleApiClient);
        personNameView.setText(person.getDisplayName());
        if (person.hasImage()) {
            Person.Image image = person.getImage();

            new AsyncTask<String, Void, Bitmap>() {
                @Override
                protected Bitmap doInBackground(String... params) {
                    try {
                        URL url = new URL(params[0]);
                        InputStream in = url.openStream();
                        return BitmapFactory.decodeStream(in);
                    } catch (Exception e) {
                    /* TODO log error */
                    }
                    return null;
                }
                @Override
                protected void onPostExecute(Bitmap bitmap) {
                    personImageView.setImageBitmap(bitmap);
                }
            }.execute(image.getUrl());
        }
   }

在此处输入链接说明

希望这有帮助

最新更新