簡體   English   中英

Android和Facebook:如何獲取登錄用戶的圖片

[英]Android and Facebook: How to get picture of logged in User

我在Android應用程序中使用官方Facebook SDK。 用戶登錄后,我可以獲取uid和facebook用戶的名稱,如下所示:

Facebook mFacebook = new Facebook(APP_ID);
// ... user logs in ...
//String jsonUser = mFacebook.request("me/picture"); // throws error
String jsonUser = mFacebook.request("me");
JSONObject obj = Util.parseJson(jsonUser);
String facebookId = obj.optString("id");
String name = obj.optString("name");

我也知道我可以使用這些鏈接訪問個人資料圖片:

https://graph.facebook.com/<facebookId>/picture 
https://graph.facebook.com/<facebookId>/picture?type=large

我很樂意使用此代碼來查看個人資料圖片:

public static Drawable getPictureForFacebookId(String facebookId) {

    Drawable picture = null;
    InputStream inputStream = null;

    try {
        inputStream = new URL("https://graph.facebook.com/" + facebookId + "/picture").openStream();
    } catch (Exception e) {        
     e.printStackTrace();
     return null;

    }
    picture = Drawable.createFromStream(inputStream, "facebook-pictures");

    return picture;
}

但它不會工作。 我總是得到以下錯誤:

SSL handshake failure: Failure in SSL library, usually a protocol error

我無法解決這個問題。 它似乎相當復雜(看這里這里 )。 那么有什么其他選項可以讓Facebook用戶成功登錄我的應用程序?

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

ID是你的個人資料ID ...

前段時間我也遇到過這個問題。 我所做的是使用異步任務下載圖片,然后使用剛下載的圖像設置ImageView。 我將粘貼代碼段:

ImageView fbUserAvatar = (ImageView) findViewById(R.id.fb_user_avatar);

private synchronized void downloadAvatar() {
    AsyncTask<Void, Void, Bitmap> task = new AsyncTask<Void, Void, Bitmap>() {

        @Override
        public Bitmap doInBackground(Void... params) {
            URL fbAvatarUrl = null;
            Bitmap fbAvatarBitmap = null;
            try {
                fbAvatarUrl = new URL("http://graph.facebook.com/"+USER_ID+"/picture");
                fbAvatarBitmap = BitmapFactory.decodeStream(fbAvatarUrl.openConnection().getInputStream());
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return fbAvatarBitmap;
        }

        @Override
        protected void onPostExecute(Bitmap result) {
            fbUserAvatar.setImageBitmap(result);
        }

    };
    task.execute();
}

這段代碼適合我。 我希望它對你也有用。

您可以請求包含您的訪問令牌的直接URl:

URL MyProfilePicURL = new URL("https://graph.facebook.com/me/picture?type=normal&method=GET&access_token="+ Access_token );

然后獲取解碼的BitMap並將其分配給圖像視圖:

Bitmap MyprofPicBitMap = null;
try {
    MyprofPicBitMap = BitmapFactory.decodeStream(MyProfilePicURL.openConnection().getInputStream());
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

MyProfilePicImageView.setImageBitmap(mIcon1);

請求(“我/圖片”)拋出一個錯誤,因為服務器返回302(重定向到圖像URL),Facebook sdk不處理這個。

要在您的應用中顯示個人資料照片,請使用Facebook SDK中的ProfilePictureView

請參閱此

只需在其上調用setProfileId(String profileId)

它將負責顯示圖像。

添加一行代碼即可解決。

HttpURLConnection.setFollowRedirects(true);

使用它,( usuario是GraphUser ):

ProfilePictureView p;
p = (ProfilePictureView) rootView.findViewById(R.id.fotoPerfil);
p.setProfileId(usuario.getId());

和xml標記:

<com.facebook.widget.ProfilePictureView
    android:id="@+id/profilePicture"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:gravity="center_horizontal"
    android:layout_marginBottom="10dp"
    facebook:preset_size="normal"/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM