我的graphrequest似乎工作正常,检索登录Names的用户也没有问题,获取他们的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;
    }




}


有另一种/更好的方法可以做到这一点吗?

非常感谢!

最佳答案

我做了这样的事情:

首先,您需要调用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;
}


希望对您有所帮助!

10-04 23:17