2017-10-20 20 views
0

アンドロイドアプリにFacebookのログイン情報を追加しようとしています。このチュートリアルhttps://www.studytutorial.in/android-facebook-integration-and-login-tutorialに従って成功しました。これには2つのアクティビティMainActivity、UserProfileActivityが含まれています。 MainActivityは、ログインボタン付きのアプリランチャーです。 UserProfileActivityがインテント経由で呼び出した正しい資格情報の後、独自のレイアウトxmlを持つため、User Profile Pic、名前、電子メールが表示されます。ログイン後にFacebookのプロフィール写真を表示するには?

戻るボタンを押した後、MainActivityに戻ります。結果の表示はloginButtonのテキストをログアウトボタンに変更したものです。ログアウトボタンを押すとログアウトが実行されますが、再度ログインすると、「以前はsome_user_nameでログインしています」というメッセージが表示されます。

これまでのログインの詳細をクリアする方法、(ちょうどloginButton上記)MainActivityのImageViewのようにプロフィールの写真を?表示する方法ここで

は私MainActivityここ

public class MainActivity extends AppCompatActivity { 
CallbackManager callbackManager; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    callbackManager = CallbackManager.Factory.create(); 
    LoginButton loginButton = (LoginButton) findViewById(R.id.login_button); 
    loginButton.setReadPermissions("email"); 
    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { 
     @Override 
     public void onSuccess(LoginResult loginResult) { 
      getUserDetails(loginResult); 
     } 
     @Override 
     public void onCancel() { 
      Log.d("LOGIN_CANCEL", "Cancel"); 
     } 
     @Override 
     public void onError(FacebookException error) { 
      Log.d("LOGIN_ERROR", "Error"); 
     } 
    }); 
} 
protected void getUserDetails(LoginResult loginResult) { 
    GraphRequest data_request = GraphRequest.newMeRequest(loginResult.getAccessToken(), 
     new GraphRequest.GraphJSONObjectCallback() { 
     @Override 
     public void onCompleted(JSONObject json_object, GraphResponse response) { 
      Intent intent = new Intent(MainActivity.this, UserProfileActivity.class); 
      intent.putExtra("userProfile", json_object.toString()); 
      startActivity(intent); 
     } 
    }); 
    Bundle permission_param = new Bundle(); 
    permission_param.putString("fields", "id, name, email, picture.width(120).height(120)"); 
    data_request.setParameters(permission_param); 
    data_request.executeAsync(); 
} 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    callbackManager.onActivityResult(requestCode, resultCode, data); 
} 

であることは私のUserProfileActivityある

public class UserProfileActivity extends AppCompatActivity implements View.OnClickListener { 
JSONObject response, profile_pic_data, profile_pic_url; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_user_profile); 
    Intent intent = getIntent(); 
    String jsondata = intent.getStringExtra("userProfile"); 
    Log.w("Jsondata", jsondata); 
    ImageView user_picture = (ImageView) findViewById(R.id.profilePic); 
    TextView user_name = (TextView) findViewById(R.id.UserName); 
    TextView user_email = (TextView) findViewById(R.id.email); 
    Button backButton = (Button) findViewById(R.id.backbutton); 
    backButton.setOnClickListener(this); 
    try { 
     response = new JSONObject(jsondata); 
     user_email.setText(response.get("email").toString()); 
     user_name.setText(response.get("name").toString()); 
     profile_pic_data = new JSONObject(response.get("picture").toString()); 
     profile_pic_url = new JSONObject(profile_pic_data.getString("data")); 
     Picasso.with(this).load(profile_pic_url.getString("url")).into(user_picture); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
} 
+0

'ProfilePictureView'を使用します。 – grant

答えて

0

あなたが

loginButton.setReadPermissions("public_profile", "email"); 

と交換し、あなたが

profile_pic_data = new JSONObject(response.get("picture").toString()); 

これをやったとき、あなたはそれを得るこの

String  sfacebookId = json_object.getString("id"); 
try { 
URL image_url = new URL("https://graph.facebook.com/" + sfacebookId + "/picture?type=large"); 
user_image = image_url.toString(); 
} catch (Exception e) { 
e.printStackTrace(); 
} 
0
loginButton.setReadPermissions("email"); 

のような外部URLとIDを使用してプロファイルの写真を呼び出す必要があります画像のURLを表します。

関連する問題