从android中的facebook相册中获取照片

时间:2010-09-14 06:52:21

标签: android facebook

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

2 个答案:

答案 0 :(得分:10)

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

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

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

然后选择相册ID,然后使用第一个电话

修改

您还需要在Permission String数组中创建Facebook对象时给予权限,还需要添加user_photos才能加载照片

答案 1 :(得分:1)

为我工作的代码。

我有两个功能 - 一个是获取专辑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;
        }
    }