如何使用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);
Picasso.with(this).load(profilePicUrl).into(profilePicIV);
中的“this”处出错了。它说 Picasso 无法应用。 - jusimenActivityName.this
代替this
。 - Charlie Niekirk