简体   繁体   English

如何从Facebook获取个人资料图片

[英]How to fetch a profile picture from facebook

I am writing an android app, in which I want to fetch a list of all my facebook-friends along with their names and profile pictures. 我正在写一个Android应用程序,我想在其中获取所有我的Facebook朋友的列表以及他们的名字和个人资料图片。

In my sample code I am getting the name of my friends but not the profile picture, please tell me the reason why i am facing this problem: 在我的示例代码中,我得到了我的朋友的名字,但没有得到个人资料图片,请告诉我为什么我面临这个问题:

       @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 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());
        }

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

Original Code is here: 原始代码在这里:

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

I have two packages in my app, first - com.january.floogoo [my custom classes package] 我的应用程序中有两个包,第一个 - com.january.floogoo [我的自定义类包]

and second - com.facebook.android [classes related to facebook SDK] 和第二 - com.facebook.android [与facebook SDK相关的类]

I am using 10 classes in com.facebook.android package, namely: 我在com.facebook.android包中使用了10个类,即:

  1. AsyncFacebookRunner.java AsyncFacebookRunner.java

  2. BaseDialogListener.java BaseDialogListener.java

  3. BaseRequestListener.java BaseRequestListener.java

  4. DialogError.java DialogError.java

  5. Facebook.java Facebook.java

  6. FacebookError.java FacebookError.java

  7. FbDialog.java FbDialog.java

  8. SessionEvents.java SessionEvents.java

  9. SessionStore.java SessionStore.java

  10. Util.java Util.java

Here my question is Am i missing something ? 我的问题是我错过了什么? If yes so please help me... 如果有,请帮助我......

Try with.. 试试......

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

Where id is one of your friend profile ID. 其中id是您的朋友个人资料ID之一。

Use the below link to fetch the profile pictures from facebook. 使用以下链接从Facebook获取个人资料图片。

https://github.com/chrtatu/FacebookFriendsList after logging in you will be getting list of friends pictures. https://github.com/chrtatu/FacebookFriendsList登录后您将获得好友图片列表。

FriendsList.Java is the class where your facebook friends get sync. FriendsList.Java是您的Facebook好友同步的类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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