如何使用Picasso [Android Studio]从Facebook获取个人资料照片

时间:2017-11-11 23:47:50

标签: java android facebook picasso

如何使用毕加索从facebook获取个人资料图片?

我已经按照本教程Can't get FB profile picture with Firebase进行操作,并且我没有编辑错误,而图片似乎并不适用。

Image of the Profile Picture

这是XML部分

<ImageButton
        android:id="@+id/profile_photo"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_below="@+id/Profile_cover"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="-60dp"
        android:elevation="5dp"
        android:padding="20dp"
        android:scaleType="centerCrop"/>

这是我的代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);

    //init Firebase\\
    auth = FirebaseAuth.getInstance();
    user = FirebaseAuth.getInstance().getCurrentUser();
    //END init Firebase\\

    //Init UI variables\\
    username = findViewById(R.id.Profile_username);
    profilePicIV = findViewById(R.id.profile_photo);
    //End Init UI variables\\


    //Init Methods\\
    setDataToValue(user);
    //End Init Methods\\

    //Facebook\\

    String profilePicUrl = "https://graph.facebook.com/" + UserID +"/picture?type=large";
    Picasso.with(this).load(profilePicUrl).into(profilePicIV);

}

private FacebookCallback<LoginResult> mCallback = new FacebookCallback<LoginResult>() {
    @Override
    public void onSuccess(final LoginResult loginResult) {
        GraphRequest request = GraphRequest.newMeRequest(
                loginResult.getAccessToken(),
                new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted(JSONObject object, GraphResponse response) {
                        Log.e("response: ", response +"");
                        try{
                            String  id = object.getString("id").toString(),
                                    email = object.getString("email").toString(),
                                    name = object.getString("name").toString(),
                                    profilePicUrl = "https://graph.facebook.com/" + loginResult.getAccessToken().getUserId() +"/picture?type=large";
                                    UserID = loginResult.getAccessToken().getUserId();
                            Log.d("imageFB", profilePicUrl);
                            Log.d("FB_ID", id);


                        }catch (Exception e){
                            e.printStackTrace();
                        }

                        finish();
                    }
                });
        Bundle params = new Bundle();
        params.putString("fields", "id,name,email,gender,birthday,friends,likes,hometown,education,work");
        request.setParameters(params);
        request.executeAsync();


    }

毕加索下载

String profilePicUrl = "https://graph.facebook.com/" + UserID +"/picture?type=large";
    Picasso.with(this).load(profilePicUrl).into(profilePicIV);

1 个答案:

答案 0 :(得分:0)

您试图在回调中分配UserID之前设置图片。

尝试在此行之后设置图像:

String  id = object.getString("id").toString(),
                                email = object.getString("email").toString(),
                                name = object.getString("name").toString(),
                                profilePicUrl = "https://graph.facebook.com/" + loginResult.getAccessToken().getUserId() +"/picture?type=large";
                                UserID = loginResult.getAccessToken().getUserId();

您需要执行此操作的原因是因为Facebook身份验证以异步方式进行,并且在调用回调之后才会初始化UserId