Android - 获取Facebook个人资料图片

Zba*_*ian 48 profile android facebook

我不知道为什么,但是当我试图获取用户的个人资料图片时,我总是变为空.我是否需要设置一些特定权限才能获得访问权限?

以下是我的方法:

public static Bitmap getFacebookProfilePicture(String userID) throws SocketException, SocketTimeoutException, MalformedURLException, IOException, Exception
{
   String imageURL;

   Bitmap bitmap = null;
   imageURL = "http://graph.facebook.com/"+userID+"/picture?type=large";
   InputStream in = (InputStream) new URL(imageURL).getContent();
   bitmap = BitmapFactory.decodeStream(in);

   return bitmap;
}

Bitmap bitmap = getFacebookProfilePicture(userId);
Run Code Online (Sandbox Code Playgroud)

我变得空了.我不知道原因是什么?任何帮助都很明显.

Jef*_*die 69

这应该工作:

public static Bitmap getFacebookProfilePicture(String userID){
    URL imageURL = new URL("https://graph.facebook.com/" + userID + "/picture?type=large");
    Bitmap bitmap = BitmapFactory.decodeStream(imageURL.openConnection().getInputStream());

    return bitmap;
}

Bitmap bitmap = getFacebookProfilePicture(userId);
Run Code Online (Sandbox Code Playgroud)

编辑:

正如@dvpublic在评论中所建议的那样,使用"https"来支持"http"来修复未下载图像的问题.

  • 我怀疑,SDK不是罪魁祸首,问题出在重定向上.尝试在浏览器中打开"http://graph.facebook.com/USER_UID/picture"链接 - 将打开"httpS://fbcdn-profile-a.akamaihd.net/..."链接.当原始协议和重定向协议相同时,自动重定向会自动生效.因此,尝试从"httpS://graph.facebook.com/USER_UID/picture"加载图像,并确保调用HttpURLConnection.setFollowRedirects(true)或conn.setInstanceFollowRedirects(true).然后BitmapFactory.decodeStream将再次工作. (9认同)
  • 在我看来,最后一个SDK使这个方法停止工作.谁知道发生了什么?BitmapFactory.decodeStream现在总是返回null (2认同)

Nik*_*rad 52

使用facebook ProfilePictureView而不是Imageview

<com.facebook.login.widget.ProfilePictureView
    android:id="@+id/friendProfilePicture"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    facebook:preset_size="small"/>
Run Code Online (Sandbox Code Playgroud)

之后你可以在代码中设置这样的facebook id

ProfilePictureView profilePictureView;

profilePictureView = (ProfilePictureView) findViewById(R.id.friendProfilePicture);

profilePictureView.setProfileId(userId);
Run Code Online (Sandbox Code Playgroud)

它的工作原理..你也可以将大小设置为小/普通/大/自定义 ProfilePictureView


小智 29

只需使用毕加索.添加Picasso Library,然后使用这个简单的行代码:

userpicture = (ImageView) row.findViewById(R.id.postuserid);

Picasso.with(context)
       .load("https://graph.facebook.com/" + userID+ "/picture?type=large")
       .into(userpicture);
Run Code Online (Sandbox Code Playgroud)


OM *_*RVI 16

获取个人资料图片网址的最佳方式

int dimensionPixelSize = getResources().getDimensionPixelSize(com.facebook.R.dimen.com_facebook_profilepictureview_preset_size_large);
Uri profilePictureUri= Profile.getCurrentProfile().getProfilePictureUri(dimensionPixelSize , dimensionPixelSize);
Run Code Online (Sandbox Code Playgroud)

要么

Uri profilePictureUri = ImageRequest.getProfilePictureUri(Profile.getCurrentProfile().getId(), dimensionPixelSize , dimensionPixelSize );
Run Code Online (Sandbox Code Playgroud)

使用Glide显示图像

Glide.with(this).load(profilePictureUri)
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .into(profilePictureView);
Run Code Online (Sandbox Code Playgroud)

没有更多的硬编码字符串

  • 还有一个内置函数可以获取 Profile 对象中的个人资料图像:Profile.getCurrentProfile().getProfilePictureUri (2认同)

Raj*_*esh 15

您必须调用GraphRequest API来获取当前个人资料图片的URL.

Bundle params = new Bundle();
params.putString("fields", "id,email,picture.type(large)");
new GraphRequest(AccessToken.getCurrentAccessToken(), "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 = BitmapFactory.decodeStream(profilePicUrl.openConnection().getInputStream());
                            // set profilePic bitmap to imageview
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
}).executeAsync();
Run Code Online (Sandbox Code Playgroud)

我希望它有所帮助!


tha*_*h84 5

注意:从2018年3月26日起,与手动链接有关的所有解决方案将不再起作用

您应在此处遵循官方指南

private static String FACEBOOK_FIELD_PROFILE_IMAGE = "picture.type(large)";
    private static String FACEBOOK_FIELDS = "fields";

    private void getFacebookData() {
        GraphRequest request = GraphRequest.newMeRequest(
                AccessToken.getCurrentAccessToken(),
                (object, response) -> {
                    updateAvatar(getImageUrl(response));
                });
        Bundle parameters = new Bundle();
        parameters.putString(FACEBOOK_FIELDS, FACEBOOK_FIELD_PROFILE_IMAGE);
        request.setParameters(parameters);
        request.executeAsync();
    }

    private static String FACEBOOK_FIELD_PICTURE = "picture";
    private static String FACEBOOK_FIELD_DATA = "data";
    private static String FACEBOOK_FIELD_URL = "url";
    private String getImageUrl(GraphResponse response) {
        String url = null;
        try {
            url = response.getJSONObject()
                    .getJSONObject(FACEBOOK_FIELD_PICTURE)
                    .getJSONObject(FACEBOOK_FIELD_DATA)
                    .getString(FACEBOOK_FIELD_URL);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return url;
    }
Run Code Online (Sandbox Code Playgroud)


Aer*_*row -1

我使用了这个代码,我得到了个人资料图片,

fbUsrPicURL = "http://graph.facebook.com" + File.separator
                    + String.valueOf(fbUID) + File.separator + "picture?type=large";
Run Code Online (Sandbox Code Playgroud)