[Android] how to get facebook profile

Bundle params = new Bundle();
params.putString("fields", "id,email,gender,cover,picture.type(large)");
new GraphRequest(token, "me", params, HttpMethod.GET,
        new GraphRequest.Callback() {
            @Override
            public void onCompleted(GraphResponse response) {
                if (response != null) {
                    try {
                        JSONObject data = response.getJSONObject();
                        if (data.has("picture")) {
                            String profilePicUrl = data.getJSONObject("picture").getJSONObject("data").getString("url");
                            Bitmap profilePic = getFacebookProfilePicture(profilePicUrl);
                            // set profilePic bitmap to imageview
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
}).executeAsync();

public static Bitmap getFacebookProfilePicture(String url){
    Bitmap bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());
    return bitmap;
}

Or:

GraphJSONObjectCallback mCallback = new GraphJSONObjectCallback()
{
            @Override
            public void onCompleted(JSONObject mData, GraphResponse mResponse) 
            {
                if(mResponse.getError() == null)
                {   
                    try
                    {
                       final JSONObject mPicture = mData.getJSONObject("picture");                  
                       final JSONObject mPictureData = mPicture.getJSONObject("data");                  
                       final boolean mSilhouette = mPictureData.getBoolean("is_silhouette");                

                      **//this is the URL to the image that you want**
                      final String mImageUrl = mPictureData.getString("url"); 
                    }

                    catch (JSONException e)
                    {
                       //JSON Error, DEBUG
                    }
                }

                else
                {
                     //Facebook GraphResponse error, DEBUG
                }
           }
}; 

Bundle mBundle = new Bundle();
mBundle.putString("fields", "picture");

GraphRequest mGetUserRequest = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), mCallback);
mGetUserRequest.setParameters(mBundle);

//if running this on the MAIN THREAD then use .executeAsync()

  

Or:

private void importFbProfilePhoto() {

    if (AccessToken.getCurrentAccessToken() != null) {

        GraphRequest request = GraphRequest.newMeRequest(
                AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted(JSONObject me, GraphResponse response) {

                        if (AccessToken.getCurrentAccessToken() != null) {

                            if (me != null) {

                                String profileImageUrl = ImageRequest.getProfilePictureUri(me.optString("id"), 500, 500).toString();
                                Log.i(LOG_TAG, profileImageUrl);

                            }
                        }
                    }
                });
        GraphRequest.executeBatchAsync(request);
    }
}

refer to :http://stackoverflow.com/questions/19855072/android-get-facebook-profile-picture

posted @ 2016-02-19 15:20  LittleEyes  阅读(593)  评论(0编辑  收藏  举报