Get user image from Facebook Graph-API
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.HttpUR开发者_StackOverflow社区LConnection.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)?
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
............................
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.
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.
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.
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);
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);
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 !
精彩评论