Can't get FB profile picture with Firebase

自闭症网瘾萝莉.ら 提交于 2019-12-19 12:00:45

问题


I cannot get user's Facebook profile picture into my imageView. I am trying to get with Picasso library. Have an idea to solve that? Here is my codes. Thanks in advance!

R.string.facebook_provider_id= "facebook.com"

Error:

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" />

回答1:


i hope you are asking permission for profile picture from facebook first, bcz without permission facebook will not allow you to get anything.

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

or you can also use facebook callback to get user profile image, like this..

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();
        }



回答2:


You can get the profile picture in good resolution with this much simple solution:

If you followed the Firebase samples, inside updateUI method use:

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

Hope it helps, as this is too much simple code.



来源:https://stackoverflow.com/questions/43260782/cant-get-fb-profile-picture-with-firebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!