6

Does anyone know if there is a way to pull a signed in users profile picture to be placed through the app, to maybe place it on the ActionBar as they navigate around?

hints, tips, examples, downloads all welcome :)

If you can help me, please assume I very little knowledge regarding anything outside basic Java!

Again, thanks people x

bfresh
  • 150
  • 3
  • 14

3 Answers3

9

You can get a user's profile image by using /1.1/users/show.json. You can refer to REST API URLs for Twitter data.

By extending TwitterApiClient we can retrieve Twitter data from the URL.

class MyTwitterApiClient extends TwitterApiClient {
    public MyTwitterApiClient(TwitterSession session) {
        super(session);
    }

    public UsersService getUsersService() {
        return getService(UsersService.class);
    }
}

interface UsersService {
    @GET("/1.1/users/show.json")
    void show(@Query("user_id") Long userId,
            @Query("screen_name") String screenName,
            @Query("include_entities") Boolean includeEntities,
            Callback<User> cb);
}

Next, get the UsersService and call its show method, passing in the defined query parameters. I defined the query parameters based on the ones that are documented.

new MyTwitterApiClient(session).getUsersService().show(12L, null, true,
new Callback<User>() {
    @Override
    public void success(Result<User> result) {
        Log.d("twittercommunity", "user's profile url is "
                + result.data.profileImageUrlHttps);
    }

    @Override
    public void failure(TwitterException exception) {
        Log.d("twittercommunity", "exception is " + exception);
    }
});

Courtesy: https://twittercommunity.com/t/android-get-user-profile-image/30579/2

Anoop M Maddasseri
  • 10,213
  • 3
  • 52
  • 73
5

This is how I got mine to work:

            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 profilebannerurl = userResult.data.profileBannerUrl;
                    String profileurl = userResult.data.profileImageUrl;

                }

                @Override
                public void failure(TwitterException e) {

                }
            });

I have place this piece of code within my LoginButton callback method:

    loginButton.setCallback(new Callback<TwitterSession>() {
        @Override
        public void success(Result<TwitterSession> result) { <insert here> }
Simon
  • 19,658
  • 27
  • 149
  • 217
  • I did exactly the same, however, i still face the callback null issue. Any idea what can be the issue? I've also added button.onActivityResult(requestCode, resultCode, data); – LEE Jun 01 '15 at 11:21
  • I think maybe check whether u had logged onto twitter before you implement any twitter functionality like fetching the profile pic etc. That is the usual why it returns null. I had struggled with this as when I started, I would log off and then still try fetch profile pic etc and it would return null as I would not have the authorisation to do so. – Simon Jun 01 '15 at 13:04
  • OK Simon. Will check that. Thanks :) – LEE Jun 01 '15 at 13:04
  • What should be the User in new Callback() ? Should it be twitter4j.User or something else? – Narendra Singh Sep 15 '15 at 12:43
  • Thats a callback method that returns an object called Result userResult. from userResults you can query it for all the user fields, by saying userResult.data.name. – Simon Sep 15 '15 at 13:01
  • I tried the same way, but neither success, nor failure getting called – Narendra Singh Sep 15 '15 at 13:55
1

I did it with a custom button and this is the code that is executed by it's onClick listener :

TwitterAuthConfig authConfig =  new TwitterAuthConfig(TWITTER_API_KEY, TWITTER_API_SECRET);
Fabric.with(activity, new Twitter(authConfig));

TwitterCore.getInstance().getApiClient().getAccountService().verifyCredentials(false, false, new com.twitter.sdk.android.core.Callback<User>() {
    @Override
    public void success(Result<User> result) {
        Log.d(TAG, "Twitter log in success");

        String userName = result.data.screenName;
        int userId = result.data.id;
        String pictureUrl = result.data.profileImageUrl;
        String coverUrl = result.data.profileBannerUrl;
    }

    @Override
    public void failure(TwitterException e) {
        Log.d(TAG, "Twitter log in error : " + e.getMessage());
    }
});

I should ask the user to authorize access to your app and log him in if he accepts.

Pafoid
  • 435
  • 4
  • 8