简体   繁体   中英

fetch photos from facebook album in android

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

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

If it's for a particular person then:

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

And then choose the album id and then use the first call

EDIT

You will also need to give permission while creating Facebook object in the Permission String array also need to add user_photos to be able to load photos

Code Worked for Me.

I have two function - one is to get Album id and anther one is just retrieving all images from that particular album.

First use test() function and then use 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();
        }
    }
} 

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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