检索Facebook个人资料图片

时间:2015-12-04 03:16:56

标签: java android facebook facebook-graph-api bitmap

我的graphrequest似乎工作正常,我没有问题检索登录名称的用户也没有任何问题获取他们的ID。我试图存储他们的个人资料图片,以便在我的应用程序中使用(通过下载它作为位图),但似乎无法成功下载它。有人能告诉我我做错了吗?

   //Run the first time we log into Facebook
    //connects everything here
    private void firstTimeFBlogin() {
        GraphRequest request = GraphRequest.newMeRequest(
                AccessToken.getCurrentAccessToken(),
                new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted(
                            JSONObject object,
                            GraphResponse response) {
                        Log.i("joe", "Heyyyyo");

                        try {

                            String userName = response.getJSONObject().getString("name");
                            String userID = response.getJSONObject().getString("id");
                            //String hi2 = response.getJSONObject().getString("first_name");
                            //String hi3 = response.getJSONObject().getString("gender");


                            final JSONObject mPicture = object.getJSONObject("picture");
                            final JSONObject mPictureData = mPicture.getJSONObject("data");

                            final String mImageUrl = mPictureData.getString("url");

                            Log.i("joe", "User's Facebook Name: " + userName);
                            Log.i("joe", ParseUser.getCurrentUser().getUsername());
                            Log.i("joe", mImageUrl);
                            Log.i("joe", userID);
                            ParseUser.getCurrentUser().put("name", userName);
                            ParseUser.getCurrentUser().put("iUrl", mImageUrl);
                            ParseUser.getCurrentUser().put("fbID", userID);

                            ParseUser.getCurrentUser().saveInBackground();

                            profilePictureRetriever(userID);

                        } catch (JSONException e) {
                            Log.i("joe", "Couldn't Succesfully retrieve the stuff...");
                            e.printStackTrace();
                        }

                    }
                });
        Bundle parameters = new Bundle();
        parameters.putString("fields", "id,name,link,picture");
        request.setParameters(parameters);
        request.executeAsync();


    }


    public void profilePictureRetriever(String id) {
        Log.i("joe", "Profile Picture Checker");
        //Bitmap bitmap = getFacebookProfilePicture(id);

        //this is here for test purposes
//tried just maually putting the url in..
        Bitmap bm = DownloadImageBitmap("https://graph.facebook.com/849993771766163/picture?type=square");

    }

    public static Bitmap DownloadImageBitmap(String url) {
        Bitmap bm = null;
        try {
            URL aURL = new URL(url);
            URLConnection conn = aURL.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            bm = BitmapFactory.decodeStream(bis);
            bis.close();
            is.close();
        } catch (IOException e) {
            Log.e("IMAGE", "Error getting bitmap", e);
        }
        return bm;
    }




}

还有其他/更好的方法吗?

非常感谢!

3 个答案:

答案 0 :(得分:3)

我做过这样的事情:

首先,您需要致电GraphRequest API以获取API也提供URL of current Profile Picture的用户的所有详细信息。

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

您可以使用此方法从我URL上面的GraphRequest API返回当前用户的个人资料图片。

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

我希望它有所帮助!

答案 1 :(得分:1)

<强>更新

在bundle中传递必需参数,因为id,name,email,gender, birthday,picture在下面的代码中传递。

LoginManager.getInstance().registerCallback(callbackManager,
                new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {
                        // App code
                        GraphRequest request = GraphRequest.newMeRequest(
                                loginResult.getAccessToken(),
                                new GraphRequest.GraphJSONObjectCallback() {
                                    @Override
                                    public void onCompleted(
                                            JSONObject object,
                                            GraphResponse response) {

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

                    @Override
                    public void onCancel() {
                        Log.e(TAG, "Cancel");
                    }

                    @Override
                    public void onError(FacebookException exception) {
                        Log.e(TAG, "Error");
                        Log.e(TAG, exception.toString());
                    }
                });

旧答案

通过致电

获取
graph.facebook.com/<FB UserId>/picture?type=large

答案 2 :(得分:0)

在登录成功后输入以下代码。

GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(),new GraphRequest.GraphJSONObjectCallback() {
                                    @Override
                                    public void onCompleted(JSONObject object,GraphResponse response) {
                                           Log.d("info",object.toString()+"");
                                           String id = object.getString("id");
                                            String profilePic ="http://graph.facebook.com/"+id+"/picture";
                                        }
                                    });
                                    Bundle parameters = new Bundle();
                                    parameters.putString("fields", "id,name,first_name,last_name,age_range,gender,locale,timezone,updated_time,verified,email");
                                    request.setParameters(parameters);
                                    request.executeAsync();