How to get profile picture in android using Twitter kit,

前端 未结 4 2069
栀梦
栀梦 2021-02-15 18:12

I\'m trying to implement a simple application that uses Twitter kit. The problem is that i\'m not able to get the profile picture.Any help would be appreciated.

Thanks<

相关标签:
4条回答
  • 2021-02-15 18:19

    from gradle 2.0.0 and up use following method:

        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) {
    
                            }
                        });
    
    0 讨论(0)
  • 2021-02-15 18:20

    Found the answer.

    There is a callback which returns you the User object.

    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) {
    
        }
    });
    

    On the success branch, you can get the User object by calling:

    User user = userResult.data;
    

    And from this object you can get all the information form the user. For the profile image:

    String profileImage = user.profileImageUrl;
    
    0 讨论(0)
  • 2021-02-15 18:27

    From the official doc:

    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”.

    Following the code:

    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);
        }
    });
    

    For further information refer to Twitter API Documentation | Profile Images and Banners

    0 讨论(0)
  • 2021-02-15 18:38

    As of Nov 2016. This works. There is a change in the implementation of verify credentials.

    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);
                            }
                        });
    
    0 讨论(0)
提交回复
热议问题