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

3

如何使用Picasso从Facebook获取个人资料图片?

我遵循了这个教程Can't get FB profile picture with Firebase,没有编译错误,但头像似乎没有被应用。

个人资料图片的图像

这是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();


    }

下载 Picasso

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

1

在回调函数中,您试图在分配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在回调被调用后才会初始化。

我在 Picasso.with(this).load(profilePicUrl).into(profilePicIV); 中的“this”处出错了。它说 Picasso 无法应用。 - jusimen
尝试使用ActivityName.this代替this - Charlie Niekirk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接