After linking Facebook login with Android, I want to get a profile picture on Facebook.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(this.getApplicationContext());
setContentView(R.layout.login_activity);
callbackManager = CallbackManager.Factory.create();
LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setReadPermissions(Arrays.asList("public_profile", "email"));
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
GraphRequest graphRequest = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
Log.v("result",object.toString());
try {
String email = object.getString("email"); // Email
String name = object.getString("name"); // name
String gender = object.getString("gender"); // Gender
String userId = object.getString("id"); //id
ImageView myImage = (ImageView)findViewById(R.id.facebookImage);
URL url = new URL("https://graph.facebook.com/"+userId+"/picture");
URLConnection conn = url.openConnection();
conn.connect();
BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
Bitmap bm = BitmapFactory.decodeStream(bis);
bis.close();
myImage.setImageBitmap(bm);
Log.d ("TAG"), "Facebook Email -> " + email);
Log.d ("TAG"), Facebook name -> " + name);
Log.d ("TAG"), "Facebook Gender -> " + gender);
} } catch (Exception e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender,birthday");
graphRequest.setParameters(parameters);
graphRequest.executeAsync();
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException error) {
Log.e("LoginErr",error.toString());
}
});
}
MyImage.setImageBitmap(bm); I changed myImage to
No change after login
android image bitmap facebook
I think there are several ways to get a picture of your Facebook account. Test using the Profile Picture View below (a widget that shows pictures when you set up your user ID).
<com.facebook.login.widget.ProfilePictureView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
facebook:preset_size="small"/>
GraphRequest request = GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject user, GraphResponse response) {
if (user != null) {
profilePictureView.setProfileId(user.optString("id"));
}
}
}).executeAsync();
Note
© 2024 OneMinuteCode. All rights reserved.