20

I want to show the users profile picture in a list view. When I try to call the graph-api from android to retrieve the image, I always get the following error.

java.io.IOException: Hostname <fbcdn-profile-a.akamaihd.net> was not verified
    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.getSecureSocket(HttpConnection.java:170)
    at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnection$HttpsEngine.connect(HttpsURLConnection.java:398)
    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.sendRequest(HttpURLConnection.java:1224)
    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.doRequestInternal(HttpURLConnection.java:1558)
    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.doRequest(HttpURLConnection.java:1551)
    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1052)
    at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnection.getInputStream(HttpsURLConnection.java:252)
    at com.facebook.android.Util.openUrl(Util.java:200)
    at com.facebook.android.Facebook.request(Facebook.java:559)

This is the code used by me:

private static void retrieveProfilePicture(String userId) throws MalformedURLException, IOException{
        facebook = FacebookHelper.getInstance();
        Bundle bundle = new Bundle();
        bundle.putString(Facebook.TOKEN, facebook.getAccessToken());
        Object picture = facebook.request("/"+userId+"/picture", bundle, "GET");

When I do the same call in the browser (https://graph.facebook.com//picture?access_token=), then I get the image returned on a url like this https://fbcdn-profile-a.akamaihd.net/...

In which format is the image delivered to me? JSON with a ref to image (url)?

一二三
  • 21,059
  • 11
  • 65
  • 74
mybecks
  • 2,443
  • 8
  • 31
  • 43

7 Answers7

83
 ImageView user_picture;
 userpicture=(ImageView)findViewById(R.id.userpicture);
 URL img_value = null;
 img_value = new URL("http://graph.facebook.com/"+id+"/picture?type=large");
 Bitmap mIcon1 = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
 userpicture.setImageBitmap(mIcon1);

Where ID is one your profile ID.

For Further details check this Reference for Graph API

............................

Venky
  • 11,049
  • 5
  • 49
  • 66
  • 3
    @FelipeConde You don't need any access token for that. You can use this URL on any browser and see that it works for any profile id – IncrediApp Aug 16 '12 at 14:12
  • 2
    @IncrediApp You are right. You just need the token when using "me" insted of user id. – Felipe Conde Aug 17 '12 at 15:01
  • @Venky I get next error: "android.os.NetworkOnMainThreadException". Have you put this code in an AsyncTask? Thanks – KryNaC Jun 03 '15 at 10:54
  • yeah, this code will block the main thread until the download is complete. Really bad, especially if you do say 30 of these request at once – slinden77 Aug 10 '16 at 12:07
16

I know this question is kind of old, but there is another way to get User's picture nowadays easily.

First, in the xml layout use:

<com.facebook.widget.ProfilePictureView
        android:id="@+id/userImage"
        android:layout_width="69dp"
        android:layout_height="69dp"
        android:layout_gravity="center_horizontal" />

Then in your fragment or activity, in the method onSessionStateChange:

private void onSessionStateChange(Session session, SessionState state,
            Exception exception) {
        if (state.isOpened()) {
            Log.i(TAG, "Logged in...");

            // Request user data and show the results
            Request.newMeRequest(session, new Request.GraphUserCallback() {
                @Override
                public void onCompleted(GraphUser user, Response response) {
                    if (user != null) {
                        //HERE: DISPLAY USER'S PICTURE
                        userPicture.setProfileId(user.getId());
                    }
                }
            }).executeAsync();

        } else if (state.isClosed()) {
            Log.i(TAG, "Logged out...");

            userPicture.setProfileId(null);
        }
    }

Hope this can help someone. I was facing the same problem and I get this post but it's 2011. Today (2013) the way of doing things has changed.

kiduxa
  • 3,339
  • 11
  • 37
  • 52
6

You can it do it by Using ProfilePictureView instead of an image view:

<com.facebook.widget.ProfilePictureView
   android:id="@+id/friendProfilePicture"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:gravity="center_horizontal"
   android:padding="5sp"
   facebook:preset_size="small" />

You can set the size to small/normal/large/custom.

Then in your code, set the user facebook id like this:

ProfilePictureView profilePictureView;
profilePictureView = (ProfilePictureView) findViewById(R.id.friendProfilePicture);
profilePictureView.setProfileId(userId);

Hope this help.

Nikhil Borad
  • 2,065
  • 16
  • 20
  • Easiest solution by far, but I think the package has moved to com.facebook.login.widget.ProfilePictureView – javijuol Jul 10 '15 at 11:36
3

There is an easy way to get facebook user image for logged in user.

To make the code work add this import statement:

import com.facebook.Profile;

See example below (in this example I use Picasso library to set image):

Uri imageUri = Profile.getCurrentProfile().getProfilePictureUri(400, 400);
Picasso.with(this).load(imageUri).into(imageView);

Numbers 400 and 400 are image width and height respectively.

JWL
  • 13,591
  • 7
  • 57
  • 63
dzikovskyy
  • 5,027
  • 3
  • 32
  • 43
2

Can be written in another way:

ImageView user_picture;
         ImageView userpicture = (ImageView)findViewById(R.id.userpicture);
         URL img_value = null;
         try {
            img_value = new URL("http://graph.facebook.com/"+"100004545962286"+"/picture?type=large");
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         Bitmap mIcon1 = null;
        try {
            mIcon1 = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         userpicture.setImageBitmap(mIcon1);
TharakaNirmana
  • 10,237
  • 8
  • 50
  • 69
2

Use below code to get user profile image with Facebook Graph API.

     ImageView profileImage = (ImageView) findViewById(R.id.profileImage);
     Bundle params = new Bundle();
     params.putBoolean("redirect", false);
     params.putString("type", "large");
          new GraphRequest(
          AccessToken.getCurrentAccessToken(),
          "me/picture",
          params,
          HttpMethod.GET,
          new GraphRequest.Callback() {
          public void onCompleted(GraphResponse response) {
          try {
            String picUrlString = (String) response.getJSONObject().getJSONObject("data").get("url");
            Glide.with(getApplicationContext()).load(picUrlString).placeholder(R.drawable.ic_launcher).into(profileImage);
          } catch (JSONException | IOException e) {
            e.printStackTrace();
        }
    }
}
).executeAsync();    

And more information refer This

OR

Simple to get user image

String UserImageUrl="https://graph.facebook.com/" + facebookUser.getFacebookID() + "/picture?type=large"; 
Glide.with(getApplicationContext()).load(UserImageUrl).placeholder(R.drawable.ic_launcher).into(profileImage); 
Ravi Vaghela
  • 3,420
  • 2
  • 23
  • 51
  • is this a better approach to get the user image rathen than using httpclient or webclient to download it? which way is the better and faster ? – CDrosos Nov 15 '16 at 15:25
0
private String facebookUser;


AccessToken token;
    token = AccessToken.getCurrentAccessToken();

    facebookUser = AccessToken.getCurrentAccessToken().getUserId();
    ProfilePictureView profilePictureView;
    profilePictureView = (ProfilePictureView) findViewById(R.id.facebookUser);

profilePictureView.setProfileId(facebookUser);

xml
<com.facebook.login.widget.ProfilePictureView
 android:id="@+id/facebookUser"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"></com.facebook.login.widget.ProfilePictureView>

hope it helps !

Enpon
  • 1