java - 检索 Facebook 个人资料图片

标签 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;
    }




}

是否有其他/更好的方法来做到这一点?

非常感谢!

最佳答案

我做过这样的事情:

首先,您需要调用 GraphRequest API 来获取用户的所有详细信息,其中 API 还提供 当前个人资料图片的 URL

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

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

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

希望对您有所帮助!

关于java - 检索 Facebook 个人资料图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34080320/

相关文章:

java - 根据查询更改 firebase 数据

java - Android:只要编辑文本字段留空,程序就会关闭并给出异常

android - 使用 facebook android sdk 应用程序崩溃 API 4 登录

java - 下一行为什么不编译?

java - 我无法将我的 id 从我的 main.xml 文件带到我的 main.java 文件

android - 作为 Android ImageSpan 源的 Internet url

javascript - 包括用于 Facebook 评论框的 JavaScript SDK

java - 如何在draw2d中给文本加下划线

java - 从 java 调试一个 dll java->jni.dll ->app.dll -> app.dll

php - 从 Facebook 帖子中删除 HTML 标签