How to get facebook profile picture of user in facebook SDK Android

前端 未结 4 779
悲哀的现实
悲哀的现实 2020-12-29 14:18

I used facebook 3.6 sdk . i want to get profile picture from graph user , last time i got image but now it returns null Bitmap.

I used following code

4条回答
  •  清酒与你
    2020-12-29 15:04

    Try this code,

    try {
            URL image_value = new URL("http://graph.facebook.com/"+ user.getId()+ "/picture?type=large");
            Bitmap bmp = null;
            try {
                    bmp = BitmapFactory.decodeStream(image_value.openConnection().getInputStream());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                    profile_pic.setImageBitmap(bmp);
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
    

    here profile_pic is your ImageView replace it with your ImageView Name.

    Edit

    Session.openActiveSession(this, true, new Session.StatusCallback() {
    
            @Override
            public void call(Session session, SessionState state,
                    Exception exception) {
                if (session.isOpened()) {
                    // make request to the /me API
                    Request.executeMeRequestAsync(session,
                            new Request.GraphUserCallback() {
                                @Override
                                public void onCompleted(GraphUser user,
                                        Response response) {
                                    if (user != null) {
                                       try {
        URL image_value = new URL("http://graph.facebook.com/"+ user.getId()+ "/picture?type=large");
        Bitmap bmp = null;
        try {
                bmp = BitmapFactory.decodeStream(image_value.openConnection().getInputStream());
            } catch (IOException e) {
                e.printStackTrace();
            }
                profile_pic.setImageBitmap(bmp);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
                                    }
                                }
                            });
                } else {
                    Toast.makeText(getApplicationContext(), "Error...",
                            Toast.LENGTH_LONG);
                }
            }
        });
    

提交回复
热议问题