如何使用 Twitter 套件获取 android 中的个人资料图片,

How to get profile picture in android using Twitter kit,

我正在尝试实现一个使用 Twitter 工具包的简单应用程序。问题是我无法获得个人资料 picture.Any 帮助将不胜感激。

谢谢

找到答案。

有一个回调,returns 你 User 对象。

TwitterCore.getInstance().getApiClient().getAccountService().verifyCredentials(true, true, false).enqueue(new Callback<User>() {
    @Override
    public void success(Result<User> result) {

    }

    @Override
    public void failure(TwitterException exception) {

    }
});

在success分支上,可以调用User获取对象:

User user = userResult.data;

并且您可以从该对象获取用户的所有信息。对于个人资料图片:

String profileImage = user.profileImageUrl;

来自官方文档:

You can obtain a user’s most recent profile image from GET users/show. Within the user object, you’ll find the profile_image_url and profile_image_url_https fields. These fields will contain the resized “normal” variant of the user’s uploaded image. This “normal” variant is typically 48x48px.

By modifying the URL, you can retrieve other variant sizings such as “bigger”, “mini”, and “original”.

代码如下:

TwitterApiClient twitterApiClient = TwitterCore.getInstance().getApiClient();
twitterApiClient.getAccountService().verifyCredentials(false, false, new Callback<User>() {
    @Override
    public void success(Result<User> userResult) {
        String name = userResult.data.name;
        String email = userResult.data.email;

        // _normal (48x48px) | _bigger (73x73px) | _mini (24x24px)  
        String photoUrlNormalSize   = userResult.data.profileImageUrl;
        String photoUrlBiggerSize   = userResult.data.profileImageUrl.replace("_normal", "_bigger");
        String photoUrlMiniSize     = userResult.data.profileImageUrl.replace("_normal", "_mini");
        String photoUrlOriginalSize = userResult.data.profileImageUrl.replace("_normal", "");
    }

    @Override
    public void failure(TwitterException exc) {
        Log.d("TwitterKit", "Verify Credentials Failure", exc);
    }
});

更多信息请参考Twitter API Documentation | Profile Images and Banners

从 gradle 2.0.0 起使用以下方法:

    Call<User> userResult=Twitter.getApiClient(session).getAccountService().verifyCredentials(true,false);
                    userResult.enqueue(new Callback<User>() {
                        @Override
                        public void success(Result<User> result) {
                            User user = userResult.data;
                            String profileImage= user.profileImageUrl;
                        }

                        @Override
                        public void failure(TwitterException exception) {

                        }
                    });

截至 2016 年 11 月。这有效。 验证凭据的实施发生了变化。

Call<User> user = TwitterCore.getInstance().getApiClient().getAccountService().verifyCredentials(false, false);
           user.enqueue(new Callback<User>() {
                       @Override
                        public void success(Result<User> userResult) {
                            String name = userResult.data.name;
                            String email = userResult.data.email;

                            // _normal (48x48px) | _bigger (73x73px) | _mini (24x24px)
                            String photoUrlNormalSize   = userResult.data.profileImageUrl;
                            String photoUrlBiggerSize   = userResult.data.profileImageUrl.replace("_normal", "_bigger");
                            String photoUrlMiniSize     = userResult.data.profileImageUrl.replace("_normal", "_mini");
                            String photoUrlOriginalSize = userResult.data.profileImageUrl.replace("_normal", "");
                        }

                        @Override
                        public void failure(TwitterException exc) {
                            Log.d("TwitterKit", "Verify Credentials Failure", exc);
                        }
                    });