안드로이드 페이스북 프로필 이미지(setImageBitmap)가 안넘어오네요..

조회수 2913회

안드로이드로 페이스북 로그인 연동한 후, 페이스북에 있는 프로필 사진을 받아오려 합니다.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(this.getApplicationContext());
        setContentView(R.layout.login_activity);

        callbackManager = CallbackManager.Factory.create();

        LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
        loginButton.setReadPermissions(Arrays.asList("public_profile", "email"));
        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                GraphRequest graphRequest = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted(JSONObject object, GraphResponse response) {
                        Log.v("result",object.toString());

                        try {
                            String email = object.getString("email");       // 이메일
                            String name = object.getString("name");         // 이름
                            String gender = object.getString("gender");     // 성별

                            String userId = object.getString("id");   //id

                            ImageView myImage = (ImageView)findViewById(R.id.facebookImage);

                            URL url = new URL("https://graph.facebook.com/"+userId+"/picture");
                            URLConnection conn = url.openConnection();
                            conn.connect();
                            BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
                            Bitmap bm = BitmapFactory.decodeStream(bis);
                            bis.close();
                            myImage.setImageBitmap(bm);

                            Log.d("TAG","페이스북 이메일 -> " + email);
                            Log.d("TAG","페이스북 이름 -> " + name);
                            Log.d("TAG","페이스북 성별 -> " + gender);

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });

                Bundle parameters = new Bundle();
                parameters.putString("fields", "id,name,email,gender,birthday");
                graphRequest.setParameters(parameters);
                graphRequest.executeAsync();
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onError(FacebookException error) {
                Log.e("LoginErr",error.toString());
            }
        });

    }

myImage.setImageBitmap(bm); 로 myImage를 바꿔줬는데도

로그인 후에도 변화가없습니다

  • (•́ ✖ •̀)
    알 수 없는 사용자

1 답변

  • 페이스북 계정의 사진을 가져오는 방법이 몇가지 존재하는 것 같은데요. 아래의 ProfilePictureView(사용자 ID를 설정하면 사진을 보여주는 위젯)를 사용하는 방법으로 테스트 해보시기 바랍니다.

    <com.facebook.login.widget.ProfilePictureView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        facebook:preset_size="small"/>
    
    GraphRequest request = GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {
            @Override
            public void onCompleted(JSONObject user, GraphResponse response) {
                if (user != null) {
                    profilePictureView.setProfileId(user.optString("id"));
                }
            }
    }).executeAsync();
    

    참고

    • (•́ ✖ •̀)
      알 수 없는 사용자

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)