在安卓设备中获取Facebook相册中的照片

3
我正在尝试从Facebook中获取特定相册的所有照片,在Android上使用Facebook Android SDK完成此任务,但问题是,我不知道需要哪个URL请求才能访问相册内的照片?
2个回答

10

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

这是一个与IT技术有关的链接,用于获取Facebook上指定相册的照片。

如果是为了特定人,则使用以下链接:

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

然后选择相册ID并使用第一个调用。

编辑

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


1
我尝试了https://graph.facebook.com/me/albums/,但它返回{ "data": [] } - 尽管我创建了一个名为“events”的相册,但数据为空。 - Hunt
这取决于相册/用户的隐私设置。 - BeRecursive
1
您需要获得用户照片权限。 - BeRecursive
@BeRecursive 你是什么意思,应该在哪里设置? - IgorGanapolsky

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;
        }
    }

2
它返回Json: {"data":[]} - Abdul Wahab
你在哪里请求了user_photos权限?这很重要! - IgorGanapolsky
1
当您尝试登录到 Facebook 时,就像在声明字段中一样 List<String> permissionNeeds = Arrays.asList("public_profile", "email", "user_posts", "user_photos", "user_birthday", " user_friends"); 并在登录时使用它,如下所示 LoginManager.getInstance().logInWithReadPermissions(this, permissionNeeds); - Sunil Chaudhary

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接