gpt4 book ai didi

java - 如何使用 Picasso [Android Studio] 从 facebook 获取个人资料图片

转载 作者:太空狗 更新时间:2023-10-29 14:41:35 24 4
gpt4 key购买 nike

如何使用 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();


}

picasso 下载

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

最佳答案

您正试图在回调中分配 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 在回调被调用之前不会被初始化。

关于java - 如何使用 Picasso [Android Studio] 从 facebook 获取个人资料图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47244031/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com