【问题标题】:Can't get FB profile picture with Firebase无法使用 Firebase 获取 FB 个人资料图片
【发布时间】:2017-09-01 19:33:16
【问题描述】:

我无法将用户的 Facebook 个人资料图片放入我的 imageView。我正在尝试使用毕加索图书馆。有解决这个问题的想法吗?这是我的代码。提前致谢!

R.string.facebook_provider_id="facebook.com"

错误:

FATAL EXCEPTION: main
Process: com.example.yunus.ototakip, PID: 3045 java.lang.RuntimeException: Unable to start activity

ComponentInfo{com.example.yunus.ototakip/com.example.yunus.ototakip.Hesabim}: java.lang.IllegalArgumentException: Target must not be null.

Hesabim.java

public class Hesabim extends AppCompatActivity {

public FirebaseAuth firebaseAuth;
public FirebaseUser user;
public CircleImageView userImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_hesabim);
    userImage= (CircleImageView) findViewById(R.id.kullaniciHesapResmi);
    firebaseAuth = FirebaseAuth.getInstance();
    user = FirebaseAuth.getInstance().getCurrentUser();
    if(firebaseAuth.getCurrentUser()==null)
    {
        finish();
        startActivity(new Intent(this,Giris.class));
    }

    kullaniciMail.setText(firebaseAuth.getCurrentUser().getEmail());
    String facebookUserId = "";
    user = FirebaseAuth.getInstance().getCurrentUser();
    for(UserInfo profile : user.getProviderData()) {
        // check if the provider id matches "facebook.com"
        if(profile.getProviderId().equals(getString(R.string.facebook_provider_id))) {
            facebookUserId = profile.getUid();
        }
    }
    String photoUrl = "https://graph.facebook.com/" + facebookUserId + "/picture?type=medium";
    Picasso.with(this).load(photoUrl).into(userImage);

hesabim.xml

        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/kullaniciHesapResmi"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/kullanici_pp"
            fancy:civ_border_color="@color/colorAccent"
            fancy:civ_border_width="2dp" />

【问题讨论】:

    标签: android facebook firebase firebase-authentication facebook-login


    【解决方案1】:

    您可以通过这个非常简单的解决方案获得高分辨率的个人资料图片:

    如果您遵循 Firebase 示例,请在 updateUI 方法中使用:

    photoUrl = user.getPhotoUrl(); //here you get the picture
    
    photoUrlstr = photoUrl.toString(); //here you store the link to quality
    
    photoUrlstr = photoUrlstr + "?height=500"; //adjust quality
    
    Glide.with(this).load(photoUrlstr).into(IVperfil); //put it in Imageview
    

    希望对您有所帮助,因为代码太简单了。

    【讨论】:

      【解决方案2】:

      我希望你先从 facebook 请求个人资料图片的许可,bcz 未经许可 facebook 不会让你得到任何东西。

      loginButton.setReadPermissions("public_profile", "email", "user_friends", "user_education_history", "user_hometown", "user_likes", "user_work_history");
      

      或者你也可以使用 facebook 回调来获取用户个人资料图片,像这样..

      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();
                                      String email = object.getString("email").toString();
                                      String name = object.getString("name").toString();
                                      String profilePicUrl = "https://graph.facebook.com/" + loginResult.getAccessToken().getUserId() + "/picture?type=large";
                                      Log.d("imageFB", profilePicUrl);
                                      Log.d("FB_ID:", id);
      
      
                                      checkFBLogin(id, email, name, profilePicUrl);
      
                                  } catch (Exception e) {
                                      e.printStackTrace();
                                  }
      
                                  finish();
                              }
      
                          });
                  Bundle parameters = new Bundle();
                  parameters.putString("fields", "id,name,email,gender,birthday,friends,likes,hometown,education,work");
                  request.setParameters(parameters);
                  request.executeAsync();
              }
      

      【讨论】:

      • 我的代码包括 loginButton.setReadPermissions("email", "public_profile");行。
      • 我应该在我的 LoginActivity 或 LoginActivity 之后打开的活动中使用您的代码吗?
      • 在登录活动中使用此代码。并将图像传递给下一个活动。
      • 是的..它是我的..使用这个函数我将 fb 数据传递给服务器。
      • 我的代码在没有这个的情况下工作,但现在当我尝试获取 Facebook 的个人资料图片时遇到问题:lookaside.facebook.com/platform/profilepic/…
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-04
      • 2019-04-08
      • 1970-01-01
      • 2020-01-05
      • 1970-01-01
      • 2013-02-05
      相关资源
      最近更新 更多