繁体   English   中英

Facebook SDK:获取相册封面照片

[英]Facebook SDK: Get album cover photo

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

我正在看如何在这里查询它 ,但是,我对如何在响应中处理它感到困惑。

这就是我到目前为止所得到的:

      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方法中处理Graph Response时,它似乎没有任何作用。

如何正确处理回复,以便获取要查询的特定专辑的封面照片。 谢谢。

问题是,对该端点进行了调用(“ /” + id +“ / picture”)- 此处的文档

是这样返回的是实际照片。 请注意,它不会返回照片或JSON对象的URL(不是内容类型的application / json),而是返回实际的照片(内容类型的image / jpeg)。 大多数标准的解析代码都不知道如何处理,这是您遇到的问题。

首先,如果您具有专辑ID,则可以在此处将被调用的端点更改为该端点。 (如果您具有照片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"
}

从那里开始,您有两种方法可以去。

做到这一点的最简单方法是简单地获取您将向其发送请求的URL并附加cover_photo id(IE- https: //graph.facebook.com/10101123455127214/picture)

并设置您要设置的照片的URL。 因此,例如,如果您使用毕加索来设置照片,它将看起来像这样:

Picasso.with(mContext).load(“ https://graph.facebook.com/10101123455127214/picture”).into(imageView );

这种方法的问题在于,您将获得质量相当低的照片,看起来像样。 超级简单的解决方法是在网址末尾附加“?type = large”

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

您将拥有一个质量更高的产品。 有关此选项的更多信息, 请参见此答案

第二种方法(需要更多的工作,但最终会有更多选择)是解析cover_photo id编号(10101123455127214)并对此端点进行单独调用它将返回如下格式的JSON:

//从照片端点返回数据

{
  "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
}

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

//静态方法/枚举:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM