2010-09-14 108 views
3

m试图从android的facebook中的愚蠢的相册中获取所有照片,我正在使用facebook android sdk来完成任务,但问题是,我不知道访问哪个url请求相册内的照片?从Facebook的相册中获取照片android

回答

10

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

如果它是那么一个特定的人:

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

,然后选择相册ID,然后使用第一个呼叫

编辑

你在创建F时也需要给予权限权限字符串数组中的acebook对象还需要添加user_photos以便能够加载照片

+1

嗯,我试过https://graph.facebook.com/me/albums/,但其返回{ “数据”:[]} - 空数据虽然我有一个相册创建的名称“事件” – Hunt 2010-09-14 09:11:06

+0

这取决于相册/用户的隐私设置 – BeRecursive 2010-09-14 13:13:26

+1

您需要user_photos权限 – BeRecursive 2010-09-14 16:27:41

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”:[]} – 2012-12-07 19:41:37

+0

你在哪里请求user_photos权限?这个很重要! – 2014-01-09 22:06:37

+1

当u尝试登录到FB,像报关备案清单 permissionNeeds = Arrays.asList( “public_profile”, “电子邮件”, \t \t \t “user_posts”, “user_photos”, “user_birthday”, “user_friends”);并使用它,而登录像这样LoginManager.getInstance()。logInWithReadPermissions(this, \t \t \t \t permissionNeeds); – 2015-09-17 11:07:01