0

我無法將用戶的Facebook個人資料圖片添加到我的imageView中。我正試圖與畢加索圖書館合作。有想法解決這個問題嗎?這是我的代碼。提前致謝!無法通過Firebase獲取FB個人資料圖片

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. 

Hesa​​bim.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" /> 

回答

1

我希望你先問自己從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(); 
     } 
+0

我的代碼,包括loginButton.setReadPermissions( 「電子郵件」, 「public_profile」);線。 –

+0

我應該在LoginActivity中使用您的代碼還是在LoginActivity後打開的活動? –

+0

在登錄活動中使用此代碼。並將圖像傳遞給下一個活動。 –

相關問題