【问题标题】:fetch photos from facebook album in android从android中的facebook相册中获取照片
【发布时间】:2010-09-14 06:52:20
【问题描述】:

我正在尝试从 android 的 facebook 获取特定相册中的所有照片,我正在使用 facebook android sdk 来完成任务,但问题是,我不知道访问相册中的照片需要什么 url 请求?

【问题讨论】:

    标签: android facebook


    【解决方案1】:

    https://graph.facebook.com/ALBUM_ID/photos

    如果是针对特定的人,那么:

    https://graph.facebook.com/me/albums/

    然后选择专辑id然后使用第一次调用

    编辑

    您还需要在权限字符串数组中创建 Facebook 对象时授予权限,还需要添加 user_photos 才能加载照片

    【讨论】:

    • 好吧,我试过 graph.facebook.com/me/albums,但它返回 { "data": [] } - 空白数据虽然我有一个专辑创建名称 "events"
    • 取决于相册/用户的隐私设置
    • 您需要 user_photos 权限
    • @BeRecursive 那是什么意思,应该在哪里设置??
    【解决方案2】:

    代码为我工作。

    我有两个功能 - 一个是获取专辑 ID,另一个是从该特定专辑中检索所有图像。

    先使用test()函数,再使用downloadpic()

    public void test()
    {
    
        facebook.getAccessToken();
    
        JSONArray albumss=null;
        String response = null;
        try {
            response = facebook.request("me/albums");
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
        JSONObject json = null;
        try {
            json = Util.parseJson(response);
        } catch (FacebookError e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        JSONArray albums = null;
        try {
            albums = json.getJSONArray("data");
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        for (int i =0; i < albums.length(); i++) {
            JSONObject album = null;
            try {
                album = albums.getJSONObject(i);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }                     
            try {
                           //Here I have selected Profile pic album.
                if (album.getString("type").equalsIgnoreCase("profile")) {
                //  JSONArray al=null;
                    wallAlbumID = album.getString("id");
                           // Now you have album id in wallAlbumID(global varible).
                    Log.d("JSON", wallAlbumID);
                    break;
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    } 
    

    现在使用downloadpic():

    void downloadpic( )
    {
    
    
        facebook.getAccessToken();
    
    
        String response = null;
        try {
            response = facebook.request(wallAlbumID+"/photos");
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        JSONObject json = null;
        try {
            json = Util.parseJson(response);
        } catch (FacebookError e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        JSONArray photos = null;
        try {
            photos = json.getJSONArray("data");
    
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        for (int i =0; i < photos.length(); i++) {
            JSONObject a = null;
            try {
                a = photos.getJSONObject(i);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }  
        String testing = null;
        try {
            testing = a.getString("source");
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    \
        URL value=null;
        try {
            value=new URL(testing);
               //Now you have URL of that particular image use it in your way
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
    
                break;
            }
        }
    

    【讨论】:

    • 它返回 Json : {"data":[]}
    • 您在哪里请求 user_photos 权限?这很重要!
    • 当您尝试登录 fb 时,例如在声明中提交 List permissionNeeds = Arrays.asList("public_profile", "email", "user_posts", "user_photos", "user_birthday", “用户朋友”);并在登录时使用它 LoginManager.getInstance().logInWithReadPermissions(this, permissionNeeds);
    猜你喜欢
    • 1970-01-01
    • 2011-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多