2016-07-28 7 views
0

"login with facebook"でアンドロイドの簡単なテストアプリケーションを作成しました。Android AppからFacebookのプロフィール画像を取得するには?

これは私のコードです:

public class MainActivity extends AppCompatActivity { 

    private TextView info; 
    private LoginButton loginButton; 
    private CallbackManager callbackManager; 

    String email; 
    String birthday; 
    String name; 
    String gender; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     FacebookSdk.sdkInitialize(getApplicationContext()); 

     setContentView(R.layout.activity_main); 

     callbackManager = CallbackManager.Factory.create(); 

     info = (TextView)findViewById(R.id.fb_text); 
     loginButton = (LoginButton)findViewById(R.id.fb_login); 
     loginButton.setReadPermissions(Arrays.asList("public_profile", "email", "user_birthday")); 


     loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { 
      @Override 
      public void onSuccess(LoginResult loginResult) { 
       info.setText(
         "User ID: " 
           + loginResult.getAccessToken().getUserId() 
           + "\n" + 
           "Auth Token: " 
           + loginResult.getAccessToken().getToken() 
       ); 

       GraphRequest request = GraphRequest.newMeRequest(
         loginResult.getAccessToken(), 
         new GraphRequest.GraphJSONObjectCallback() { 
          @Override 
          public void onCompleted(JSONObject object, GraphResponse response) { 
           Log.v("LoginActivity", response.toString()); 

           // Application code 
           try { 
            email = object.getString("email"); 
            birthday = object.getString("birthday"); // 01/31/1980 format 
            name = object.getString("name"); 
            gender = object.getString("gender"); 
           } catch (JSONException e) { 
            e.printStackTrace(); 
           } 

           info.append(
             "User Name: " + name 
               + "\n" + 
               "birthday: " 
               + birthday 
               + "\n" + 
               "Gender: " 
               + gender 
               + "\n" + 
               "Email: " 
               + email 
           ); 
          } 
         }); 
       Bundle parameters = new Bundle(); 
       parameters.putString("fields", "id,name,email,gender,birthday"); 
       request.setParameters(parameters); 
       request.executeAsync(); 
      } 

      @Override 
      public void onCancel() { 
       info.setText("Login Cancelled"); 

      } 

      @Override 
      public void onError(FacebookException e) { 
       info.setText("Login Failed"); 
      } 
     }); 


    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     callbackManager.onActivityResult(requestCode, resultCode, data); 
    } 
} 

iは、ユーザーのプロフィール画像を取得し、私の活動にそれを表示するために何をすべき?

+0

使用FacebookのAPI: "https://graph.facebook.com/" +: https://developers.facebook.com/docs/graph-api/reference/user/picture/ – Uata

+0

これを使用しますfbuserのid + "/ picture?type = large" –

答えて

4

ちょうどあなたの既存のコードに

parameters.putString("fields", "id,name,email,gender,birthday,picture.type(large)"); 

picture.typeを次のパラメータを追加します(大)ところで、あなたのプロフィール画像

でURLが返されますと、あなたは、Facebookで返さJSONObjectからそのURLを取得することができます

profilePicUrl = object.getJSONObject("picture").getJSONObject("data").getString("url"); 
2

fbユーザーのIDを取得してから、そのIDでプロファイルを取得してください。場所でProfilePictureViewを使用

name = object.getString("name"); 
facebook_id = object.getString("id"); 

第一の方法

使用によりLOGGEDINユーザーのfacebook_idを取得

"https://graph.facebook.com/"+id of user+"/picture?type=large" 
2

- :

GraphRequest request = new GraphRequest(
       AccessToken.getCurrentAccessToken(), 
       "/me", 
       null, 
       HttpMethod.GET, 
       new GraphRequest.Callback() { 
        public void onCompleted(GraphResponse response) { 
      /* handle the result */ 
         try { 
          Logs.log(TAG, response.getJSONObject().toString()); 
          String id = response.getJSONObject().getString("id"); 
          String email = response.getJSONObject().getString("email"); 
          String name = response.getJSONObject().getString("name"); 

         } catch (JSONException e) { 
          Logs.log(e); 
         } 
        } 
       } 
     ); 
     Bundle parameters = new Bundle(); 
     parameters.putString("fields", "id,name,email"); 
     request.setParameters(parameters); 
     request.executeAsync(); 

その後ユーザーのプロファイルpicがになります

<com.facebook.login.widget.ProfilePictureView 
     android:id="@+id/image" 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:layout_centerInParent="true" /> 
そして、このラインでこれにfacebook_idを使用してProfilePic設定

profilePictureView.setProfileId(facebook_id); 

第二の方法

facebook_idを使用してユーザープロファイルPICのビットマップを取得し、ImageViewのには、このビットマップを設定します。

URL img_value = new URL("https://graph.facebook.com/" + facebook_id + "/picture?type=large"); 

Bitmap mIcon = BitmapFactory.decodeStream(img_value.openConnection().getInputStream()); 
関連する問題