Facebook SDK:获取专辑封面照片

时间:2016-03-10 11:31:01

标签: android facebook facebook-graph-api

我正在尝试使用Facebook sdk获取专辑的封面照片网址。

我正在关注how to query it here,但是,我对如何在响应中处理它感到困惑。

这是我迄今为止所得到的:

      public void getAlbumCoverPhoto(String id){

    Bundle params = new Bundle();
    params.putString("type", "small"); //You use this to get a pre-specified size of picture.
    params.putBoolean("redirect", false);
    new GraphRequest(
            AccessToken.getCurrentAccessToken(),
            "/" + id + "/picture",
            params,
            HttpMethod.GET,
            new GraphRequest.Callback() {
                public void onCompleted(GraphResponse response) {

                    try {
                        if (response.getError() == null) {
                            JSONObject joMain = response.getJSONObject();
                            if (joMain.has("data")) {

                             System.out.println(joMain);
                              //How can I get the photo source from here?
                            }

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

                }
            }).executeAsync();

}

但是当我试图在onCompleted方法中处理图形响应时,它似乎没有做任何事情。

如何正确处理响应,以便获取我正在查询的特定相册的封面照片。谢谢。

1 个答案:

答案 0 :(得分:0)

问题是对该端点进行了调用 (“/”+ id +“/ picture”) - Documentation here

这是否会返回实际照片。注意,它不返回照片或JSON对象的URL(非内容类型的应用程序/ json),而是返回实际照片(内容类型图像/ jpeg)。大多数标准的解析代码都不知道如何处理它,这就是你遇到的问题。

首先,如果您有相册ID,则可以将被调用的端点更改为this one here。 (如果您有照片ID或cover_photo ID,则可以跳过此步骤) 此端点返回一个可解析的对象,结构如下:

// JSON

{
  "id": "101012345368815924",
  "can_upload": false,
  "count": 32,
  "cover_photo": "10101123455127214",
  "created_time": "2010-12-31T02:06:45+0000",
  "from": {
    "name": "Bob Bobson",
    "id": "10103912345123454"
  },
  "link": "https://www.facebook.com/album.php?fbid=10100099323315924&id=10103924111095954&aid=2622251",
  "name": "Mobile Uploads",
  "privacy": "everyone",
  "type": "mobile",
  "updated_time": "2014-10-17T01:11:18+0000"
}

从那里,你有2种方法可以去。

最简单的方法是简单地获取您要发送请求的URL并附加cover_photo id (IE - https://graph.facebook.com/10101123455127214/picture

并设置您正在设置的照片的网址。因此,例如,如果您使用毕加索设置该照片,它将如下所示:

Picasso.with(mContext).load( “https://graph.facebook.com/10101123455127214/picture”)代入(ImageView的);

这种方法的问题在于你会得到质量相当低的照片,看起来会像素化。超级简单的修复是将“?type = large”附加到url的末尾

https://graph.facebook.com/10101123455127214/picture?type=large

你将拥有一个质量更好的工作。 (See this answer除了大的选项以外的更多选项)

第二种方式(更多工作,但最后有更多选项)是解析cover_photo ID号码(10101123455127214)并单独调用to this endpoint,它将返回格式如下:

//从照片端点返回数据

{
  "id": "10101123123027214",
  "created_time": "2013-04-14T03:41:17+0000",
  "from": {
    "name": "Bob Bobson",
    "id": "10101231235095954"
  },
  "height": 720,
  "icon": "https:\/\/static.xx.fbcdn.net\/rsrc.php\/v2\/yz\/r\/StEh333Pvjk.gif",
  "images": [
    {
      "height": 1632,
      "source": "https:\/\/scontent.xx.fbcdn.net\/t31.0-8\/891691_101015391232137214_536409052_o.jpg",
      "width": 1224
    },
    {
      "height": 1280,
      "source": "https:\/\/scontent.xx.fbcdn.net\/t31.0-8\/p960x960\/891691_1123155327214_536409052_o.jpg",
      "width": 960
    },
    {
      "height": 960,
      "source": "https:\/\/scontent.xx.fbcdn.net\/v\/t1.0-9\/69013_10101539155123127214_536409052_n.jpg?oh=079600f24b8c9923891232134f9ccb5fce&oe=57CFF2C5",
      "width": 720
    },
    {
      "height": 800,
      "source": "https:\/\/scontent.xx.fbcdn.net\/t31.0-0\/p600x600\/891691_1010112313155327214_536409052_o.jpg",
      "width": 600
    },
    {
      "height": 640,
      "source": "https:\/\/scontent.xx.fbcdn.net\/v\/t1.0-0\/p480x480\/69013_1010100031235327214_536409052_n.jpg?oh=b582ff84568f5ac523132156a7ab0a735&oe=57CC1B81",
      "width": 480
    },
    {
      "height": 426,
      "source": "https:\/\/scontent.xx.fbcdn.net\/v\/t1.0-0\/p320x320\/69013_10101520000139155327214_536409052_n.jpg?oh=b0ca4c70ce79574664a28223134c993&oe=5801544D",
      "width": 320
    },
    {
      "height": 540,
      "source": "https:\/\/scontent.xx.fbcdn.net\/v\/t1.0-0\/p180x540\/69013_10101539000333355327214_536409052_n.jpg?oh=2f403ed6447da8caa22222aaf00754ee&oe=5809FD06",
      "width": 405
    },
    {
      "height": 173,
      "source": "https:\/\/scontent.xx.fbcdn.net\/v\/t1.0-0\/p130x130\/69013_10101533339005327214_536409052_n.jpg?oh=cc05c51315d409222227a62d7356&oe=58089B0C",
      "width": 130
    },
    {
      "height": 225,
      "source": "https:\/\/scontent.xx.fbcdn.net\/v\/t1.0-0\/p75x225\/69013_101015322200327214_536409052_n.jpg?oh=0d05a9718fafc27c33336fce7acb69f&oe=57D1C45D",
      "width": 168
    }
  ],
  "link": "https:\/\/www.facebook.com\/photo.php?fbid=1010153000005327214&set=a.1013330000008815924.20000351.3300000005&type=3",
  "name": "info from the picture will appear here",
  "picture": "https:\/\/scontent.xx.fbcdn.net\/v\/t1.0-0\/s130x130\/69013_10101400044327214_536400052_n.jpg?oh=01ff46df0000641d2a954444000004bd873&oe=57C8A768",
  "source": "https:\/\/scontent.xx.fbcdn.net\/t31.0-8\/s720x720\/891691_10101539444427214_5000409052_o.jpg",
  "updated_time": "2013-04-14T03:41:17+0000",
  "width": 540
}

从这里,您可以看到您现在如何根据需要选择多个照片网址(尺寸方式)。我在这里使用的最后一个技巧是我有一个静态方法来获得最合适的照片。这是代码:

//静态方法/枚举:

public enum PHOTO_QUALITY {LOW, MEDIUM, HIGH}
    /**
     * Takes in a parsed object from a facebook return and gets the image URL out of it
     * @param photoObject The parsed object returned from FB
     * @param quality Quality to return. If null, will set to low
     * @return String url to the actual photo
     */
    public static String getImageUrlFromObject(FacebookDataObject.FacebookPhotoObject photoObject,
                                               PHOTO_QUALITY quality){
        if(photoObject == null){
            return null;
        }
        if(quality == null){
            quality = PHOTO_QUALITY.LOW;
        }

        String urlToReturn = null;

        FacebookDataObject.FacebookImage[] images = photoObject.getImages();
        if(images == null){
            return  null;
        }
        int size = images.length;
        if(size == 0){
            return null;
        }
        if(size == 1){
            urlToReturn = images[0].getSource();
            return urlToReturn;
        }
        if(size > 1){ 

            try{
                //This switch assumes that FB will keep sending back photos in reducing quality (high to low)
                switch (quality){
                    case HIGH:
                        urlToReturn = images[0].getSource(); //Top of the lsit
                        break;

                    case MEDIUM:
                        int pickme1 = size / 2; //Middle of the list
                        urlToReturn = images[pickme1].getSource();
                        break;

                    case LOW:
                        int pickme2 = size - 1 ; //End of the list
                        urlToReturn = images[pickme2].getSource();
                        break;
                }
            } catch (Exception e){
                urlToReturn = images[0].getSource();
                return urlToReturn;
            }

        }
        return urlToReturn;
    }

那应该为你做。祝你好运。

-PGMac