2013-01-08 124 views
1

我正在编写一个android应用程序,在该应用程序中,我想获取我的所有Facebook朋友以及他们的姓名和个人资料照片的列表。如何从Facebook获取个人资料图片

在我的示例代码,我收到朋友的名字,但没有资料图片,请告诉我为什么我面对这个问题的原因:

 @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
    View v = convertView; 

    if (v == null) { 
     v = inflater.inflate(R.layout.contact_tab, parent, false); 
    } 

    if (position >= myFriends.size()) { 
     return v; 
    } 

    MyFriend friend = myFriends.get(position); 
    ImageView icon_pic = (ImageView) v.findViewById(R.id.icon_pic); 
    try { 
     URL url = new URL(friend.getPic()); 
     InputStream is = (InputStream) url.getContent(); 
     Drawable image = Drawable.createFromStream(is, "fb"); 
     icon_pic.setImageDrawable(image); 
    } catch (Exception e) { 
     Log.v(TAG, "myadapter: " + e.getMessage()); 
    } 

MyFacebook.java

public void init(Main main) { 
    this.main = main; 
    mFacebook = new Facebook(APP_ID); 
    isReady = false; 
    mAsyncRunner = new AsyncFacebookRunner(mFacebook); 
    mFacebook.authorize(main, new String[] { "publish_stream", 
      "friends_birthday", "friends_photos" }, new MyAuthorizeListener()); 
} 

// ref- http://developers.facebook.com/docs/reference/api/user/ 
public Report reLoadAllFriends() { 
    if (!isReady) { 
     Log.v(TAG, "myfacebook.reloadallfriends Not ready yet!"); 
     return new Report(false, "Not ready yet!"); 
    } 

    Bundle params = new Bundle(); 
    params.putString("fields", "id,name,birthday,picture"); 
    mAsyncRunner.request("me/friends", params, new MyRequestListener(
      RequestType.FRIEND_LIST)); 

    Log.v(TAG, "myfacebook.reloadallfriends Fetch started."); 
    return new Report(true, "Fetch started"); 
} 

public List<MyFriend> getAllFriends() { 
    return getFilteredFriends(null); 
} 

public List<MyFriend> getFilteredFriends(com.january.floogoo.Filter week) { 
    return Main.db.getFriendsFilteredBy(week); 
} 

public List<Map<String, String>> getAllFriendsAsMap() { 
    return getFilteredFriendsAsMap(null); 
} 

public List<Map<String, String>> getFilteredFriendsAsMap(Filter filterBy) { 
    List<MyFriend> friendList = Main.db.getFriendsFilteredBy(filterBy); 

    List<Map<String, String>> list = new ArrayList<Map<String, String>>(); 
    for (MyFriend friend : friendList) { 
     list.add(friend.getMap()); 
    } 
    return list; 
} 

public void post(String receiver, String message) { 
    if (isReady) { 
     Bundle params = new Bundle(); 
     params.putString("message", message); 

     mAsyncRunner.request(receiver + "/feed", params, "POST", 
       new MyRequestListener(RequestType.FEED_POST)); 
    } 
} 

class MyAuthorizeListener extends BaseDialogListener { 
    public void onComplete(Bundle values) { 
     Log.i(TAG, "Authorization successfull"); 
     isReady = true; 
     main.loadContents(); 
    } 
} 

class MyRequestListener extends BaseRequestListener { 
    private RequestType type; 

    public MyRequestListener(RequestType type) { 
     this.type = type; 
    } 

    public void onComplete(final String response) { 
     try { 
      switch (type) { 
      case FRIEND_LIST: 
       Log.d(TAG, "myfacebook.friendlist Response: " 
       + response.toString()); 
       myFriends.clear(); 
       JSONArray jarr = Util.parseJson(response).getJSONArray(
         "data"); 
       for (int i = 0; i < jarr.length(); i++) { 
        JSONObject json = jarr.getJSONObject(i); 
        String fbID = json.getString("id"); 
        String name = json.getString("name"); 
        String bday = json.optString("birthday"); 
        String pic = json.getString("picture"); 

        myFriends.add(new MyFriend(fbID, name, bday, pic)); 
       } 
       main.notifyMain(Note.FRIENDLIST_RELOADED); 
       break; 
      case FEED_POST: 
       Log.d(TAG, "myfacebook.feedpost Response: " 
         + response.toString()); 
       break; 
      default: 
       break; 
      } 
     } catch (JSONException e) { 
      Log.e(TAG, "JSONException: " + e.getMessage()); 
     } catch (FacebookError e) { 
      Log.e(TAG, "FacebookError: " + e.getMessage()); 
     } 
    } 
} 

================================================= =================

ImageView icon_pic = (ImageView) v.findViewById(R.id.icon_pic); 
    try{ 
    URL img_value = null; 
    String id = null; 
img_value = new URL 
     ("http://graph.facebook.com/"+id+"/picture?type=large");    
     Bitmap mIcon = BitmapFactory.decodeStream 
     (img_value.openConnection().getInputStream()); 
    icon_pic.setImageBitmap(mIcon); 
    }catch (Exception e) { 
      Log.v(TAG, "myadapter: " + e.getMessage()); 
     } 

==== ================================================== ================

原始代码是在这里:

https://github.com/prajwol/Birthday-Reminder

我有两个包在我的应用程序,第一 - com.january.floogoo [我的自定义类的包]

和第二 - com.facebook.android [类相关的Facebook SDK]

我正在使用10班com.facebook.android包,即:

  1. AsyncFacebookRunner.java

  2. BaseDialogListener.java

  3. BaseRequestListener.java

  4. DialogError.java

  5. Facebook.java

  6. FacebookError.java

  7. FbDialog.java

  8. SessionEvents.java

  9. SessionStore.java

  10. Util.java

这里我的问题是我缺少的东西?如果是,请帮助我...

+0

@Hardy日Thnx编辑 – Liza

+0

你在这段代码中面临哪些问题?你试试下面我的代码,查看个人资料照片?这里查看logcat的意见,如果这些代码导致错误。 – ridoy

+0

首先告诉,这个代码有什么问题,其次发布logcat如果有任何错误。 –

回答

1

使用以下链接从Facebook获取个人资料图片。

https://github.com/chrtatu/FacebookFriendsList登录后,会得到朋友的图片列表中。

FriendsList.Java是你的Facebook朋友得到同步的类。

+0

如果我也想获取dob和朋友的名字,然后我必须使用什么源代码或像你所显示的任何链接最后一个... – Liza

+0

从上面你会得到朋友列表以及个人资料图片,相同的这些东西,你必须添加生日获取出生数据的权限。 – itsrajesh4uguys

+0

您需要根据出生日期在列表视图中进行排序...(日期在java中排序) – itsrajesh4uguys

2

尝试用..

ImageView icon_pic = (ImageView) v.findViewById(R.id.icon_pic); 
URL img_value = null; 
img_value = new URL("http://graph.facebook.com/"+id+"/picture?type=large"); 
Bitmap mIcon = BitmapFactory.decodeStream(img_value.openConnection().getInputStream()); 
icon_pic.setImageBitmap(mIcon); 

其中,ID是你的朋友的个人资料ID之一。

相关问题