无法使用fb登录检索个人资料照片

时间:2014-07-30 10:29:08

标签: android facebook image facebook-graph-api login

我在官方网页上关注了facebook的教程:

Facebook Login tutorial

但是,在我完成此操作后,我可以登录,但它无法检索我的个人资料图片。其他人经历过这个或者有什么可能导致这种情况的想法?我没有任何错误..

4 个答案:

答案 0 :(得分:2)

我认为你很清楚facebook useUserId和FacebookToken。

个人资料图片获取可以通过两种方式完成:

1:Facebook为您提供了个人资料图片的视图,即ProfilePictureView

代替xml布局文件中的imageview采用此视图。

在此视图中加载图像时,只需执行

ProfilePictureView profilePictureView;
        profilePictureView = (ProfilePictureView) findViewById(R.id.friendProfilePicture);
        profilePictureView.setCropped(true);
        profilePictureView.setProfileId(USER_ID);

2:另一个选项是在Facebook令牌

的帮助下获取个人资料照片
ImageView imgProfilePic12=(ImageView)findViewById(R.id.imgProfilePic);

new LoadProfileImage(imgProfilePic12)
                .execute("https://graph.facebook.com/me/picture?type=normal&method=GET&access_token="
                        + Faceboo_Access_Token);


/**
     * Background Async task to load user profile picture from url
     * */
    private class LoadProfileImage extends AsyncTask<String, Void, Bitmap> {
        ImageView bmImage;

        public LoadProfileImage(ImageView bmImage) {
            this.bmImage = bmImage;

        }

        protected Bitmap doInBackground(String... urls) {

            String urldisplay = urls[0];
            Bitmap mIcon11 = null;

            try {

                InputStream in = new java.net.URL(urldisplay).openStream();
                mIcon11 = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }

            return mIcon11;
        }

        protected void onPostExecute(Bitmap result) {
            bmImage.setImageBitmap(result);
        }
    }

答案 1 :(得分:1)

您是否已成功登录Facebook? 如果是,那么

try {
                            JSONObject fbResObj = new JSONObject(fbUser
                                    .getInnerJSONObject().toString());
                            String id = fbResObj.getString("id");
                            url = new URL("http://graph.facebook.com/" + id
                                    + "/picture?style=small");
                            Log.v(TAG, url.toString());
                            bmp = BitmapFactory.decodeStream(url
                                    .openConnection().getInputStream());


                        } catch (Exception e) {
                            e.printStackTrace();
                        }

试试这个,其中fbUser是GraphUser的对象,你继续执行GraphUserCallBack,

答案 2 :(得分:1)

 private void getUserDetail() {

        String fqlQuery = "SELECT name,uid,pic  FROM user WHERE uid in (SELECT uid1 FROM friend WHERE uid1=me())";
        Bundle _params = new Bundle();
        _params.putString("q", fqlQuery);
        Session _session = Session.getActiveSession();
        Request _request = new Request(_session, "/fql", _params, HttpMethod.GET, new Request.Callback() {
            public void onCompleted(Response response) {

                GraphObject graphObject = response.getGraphObject();
                if (graphObject != null) {
                    if (graphObject.getProperty("data") != null) {
                        try {
                            String _arry = graphObject.getProperty("data").toString();
                            JSONArray _jsonArray = new JSONArray(_arry);
                            if(_jsonArray.length()==1)
                            {
                                //here u will get your user's userId and Profile Picture and Name
                            String _uid = _jsonObject.getString("uid");
                            String _Name = _jsonObject.getString("name");
                            String _ImagePath = _jsonObject.getString("pic");
                            }

                                }


                        } catch (JSONException ex) {
                            if (ex != null) {
                                Toast.makeText(getApplicationContext(), ex.getMessage(), Toast.LENGTH_LONG).show();
                            }
                        }
                    }
                }
            }
        });
        Request.executeBatchAsync(_request);

    }

答案 3 :(得分:-1)

如果您有用户的Facebook ID来获取图片,请使用此链接

https://graph.facebook.com/ FACEBOOK_USER_ID /画面类型=大“;

小图片使用type = small