Android get user profile image


#1

hi ,
i want to get user profile image using fabric api in android


#2

Hello,

Are you wanting to get the profile image of the logged in user? If so, you can get this by calling AccountServices#verifyCredentials(), which will return the user object associated with the specified TwitterSession.

However, if you want to get the profile image of any Twitter user, there isn’t a method out of the box that allows you to do this. You’ll need to extend the TwitterApiClient to add additional Twitter authenticated endpoints. Below is an example of how you can get a user’s profile image by using the /1.1/users/show.json endpoint.

First, extend the TwitterApiClient to provide a UsersService.

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

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

Hope this helps!


How can i get Profile image of logged in user
#3

Hey @lientm delighted to hear there’s a way to do this!

Could you be more detailed in your answer please? I’m relatively new to developing and would love to implement this into my application.

Thanks!


#4

I actually think @lientm has given a really detailed answer here - what specific part of the code is causing you confusion, so we can assist?

The final URL to the profile image will be logged by this method here:

    @Override
    public void success(Result<User> result) {
        Log.d("twittercommunity", "user's profile url is "
                + result.data.profileImageUrlHttps);
    }

#5

hey @andypiper thanks for helping me, but I’m finding it hard to understand the code snippet above!
I am unsure in terms of what imports are needed, where in the in class it is located, and also the tag!
Basic items but I am pretty new to this.

Thanks :slight_smile:


#6

Hi lientm

Do you have to get permission first (from Twitter, i.e. get app white listed) to access a user’s profile image as I’ve tried the code above, and I can’t access the image’s url. I am only interested in getting the profile image of the logged in user as you mentioned in your first paragraph. I have tried verifyCredentials(), but I can’t get that to work either, see code below. I’m sure I’m doing something obviously wrong.

            Twitter.getApiClient(session).getAccountService()
                    .verifyCredentials(true, false, new Callback<User>() {

                        @Override
                        public void failure(TwitterException e) {

                        }

                        @Override
                        public void success(Result<User> userResult) {

                            User user = userResult.data;
                            twitterImage = user.profileImageUrl;

                        }

                    });

Thanks.


#7

You do not need any whitelisting to use this. In order to get the profile image though, you can’t rely on it being inside the verifyCredentials success callback, you need to define a new service with TwitterApiClient to hit the /1.1/users/show.json endpoint as described above.


#8

I’m using Fabric API. Based on this link Twitter Fabric API get Profile Image of various sizes

How can i get the original image (the one with the largest size).

Right now, i’m fetching all the data through the user object

 User user = userResult.data;
twitterImage = user.profileImageUrl;

However, it returns the default image with size 48x48.
I want this one Twitter profile image with largest size


#9

Looking into the Android Twitter Kit code (which is open source on GitHub) it does look like it only grabs the “normal” image and not the original.


#10

So, what’s the solution? How do i get the larger profile image?


#11

You’d have to use the APIClient methods to call users/show.json and then retrieve the URL for the profile image, and modify the URL to point at the original. Then, retrieve that directly in code.


#12

If you can show me some code please, because theoretically whatever you said is already mentioned in the docs, and i’ve already read that. But the manipulation of the URL is something that i don’t know how to do!


#13

I am really keen to be able to get the highest resolution possible of the profile picture.
48x48 serves to nothing, unfortunately. Specially with Retina devices these days.

Can anyone please help?

Thanks.


#14

Ok! It’s quite simple. Since i’m using Java, i just called .replace() method.

  twitterImage = user.profileImageUrl.replace("_normal", ""); --> this gives me the original image size.

There are 2 patterns for the URL.

  1. URL ends with _normal (before dot image extension) --> returns 48x48
  2. URL ends with _bigger (before dot image extension) —> returns 73x73

And if you want a bigger size than the above tow, then just remove the underscore and the corresponding text after that. Done !


#16

Hi when i am using the above procedure , i am getting jack is user name and Profile pic as below
https://pbs.twimg.com/profile_images/658036183713251328/UGqRFh72_normal.jpg

Please help me where it went wrong.

Update: It Got solved with verifyCredentials
i.e.Twitter.getApiClient(session).getAccountService()
.verifyCredentials(true, false, new Callback()


#17