[英]How to get facebook profile picture of user in facebook SDK 3.0 Android
[英]How to get facebook profile picture of user in facebook SDK Android
我使用facebook 3.6 sdk。 我想從圖形用戶獲取個人資料圖片,上次我有圖像,但現在它返回空位圖。
我使用以下代碼
private void onSessionStateChange(Session session, SessionState state,
Exception exception) {
if (session.isOpened()) {
Request.newMeRequest(session, new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
try {
URL imgUrl = new URL("http://graph.facebook.com/"
+ user.getId() + "/picture?type=large");
InputStream in = (InputStream) imgUrl.getContent();
Bitmap bitmap = BitmapFactory.decodeStream(in);
//Bitmap bitmap = BitmapFactory.decodeStream(imgUrl // tried this also
//.openConnection().getInputStream());
} catch (Exception e) {
e.printStackTrace();
}
}
}
}).executeAsync();
}
}
當我使用直接鏈接然后它工作。
imgUrl = new URL("https://fbcdn-dragon-a.akamaihd.net/hphotos-ak-ash3/t39.2365-6/851558_160351450817973_1678868765_n.png");
我也提到了這個圖譜API參考
當原始協議和重定向協議相同時,自動重定向會自動生效。
因此,嘗試從https而不是http :“ https://graph.facebook.com/USER_ID/picture ”加載圖像; 因為圖片的網址是“ https://fbcdn-profile-a.akamaihd.net/ ....”
然后BitmapFactory.decodeStream
將再次工作。
試試這段代碼,
try {
URL image_value = new URL("http://graph.facebook.com/"+ user.getId()+ "/picture?type=large");
Bitmap bmp = null;
try {
bmp = BitmapFactory.decodeStream(image_value.openConnection().getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
profile_pic.setImageBitmap(bmp);
} catch (MalformedURLException e) {
e.printStackTrace();
}
這里profile_pic
是你的ImageView
用你的ImageView
名稱替換它。
編輯
Session.openActiveSession(this, true, new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state,
Exception exception) {
if (session.isOpened()) {
// make request to the /me API
Request.executeMeRequestAsync(session,
new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user,
Response response) {
if (user != null) {
try {
URL image_value = new URL("http://graph.facebook.com/"+ user.getId()+ "/picture?type=large");
Bitmap bmp = null;
try {
bmp = BitmapFactory.decodeStream(image_value.openConnection().getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
profile_pic.setImageBitmap(bmp);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
});
} else {
Toast.makeText(getApplicationContext(), "Error...",
Toast.LENGTH_LONG);
}
}
});
試試這個代碼
public static String getProfilePicture() {
String stringURL = null;
try {
stringURL = "http://graph.facebook.com/" + URLEncoder.encode(DataStorage.getFB_USER_ID(), "UTF-8") + "?fields=" + URLEncoder.encode("picture", "UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
LogUtil.log(TAG, "getProfilePicture final url is : "+stringURL);
JSONObject jsonObject = null;
String response = "";
try {
HttpGet get = new HttpGet(stringURL);
get.setHeader("Content-Type", "text/plain; charset=utf-8");
get.setHeader("Expect", "100-continue");
HttpResponse resp = null;
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
resp = httpClient.execute(get);
} catch (Exception e) {
e.printStackTrace();
}
// get the response from the server and store it in result
DataInputStream dataIn = null;
try {
// dataIn = new DataInputStream(connection.getInputStream());
if (resp != null) {
dataIn = new DataInputStream((resp.getEntity().getContent()));
}
}catch (Exception e) {
e.printStackTrace();
}
if(dataIn != null){
String inputLine;
while ((inputLine = dataIn.readLine()) != null) {
response += inputLine;
}
if(Constant.DEBUG) Log.d(TAG,"final response is : "+response);
if(response != null && !(response.trim().equals(""))) {
jsonObject = new JSONObject(response);
}
dataIn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
String profilePicture = "";
try{
if(jsonObject != null){
JSONObject jsonPicture = jsonObject.getJSONObject("picture");
if(jsonPicture != null){
JSONObject jsonData = jsonPicture.getJSONObject("data");
if(jsonData != null){
profilePicture = jsonData.getString("url");
}
}
}
}catch (Exception e) {
e.printStackTrace();
}
LogUtil.log(TAG, "user fb profile picture url is : "+profilePicture);
return profilePicture;
}
我試圖使用重定向頁面“ https://fbcdn-profile-a.akamaihd.net/”+ USERID +“/ picture?type = large”但它沒有用。
現在看來facebook會將你重定向到我們無法猜到的不同頁面。 URL中的隨機變量種類。
因此,請嘗試以下方法獲取facebook提供的新重定向頁面。
private String getProfileGif(String userId) throws IOException {
HttpParams httpParams = new BasicHttpParams();
httpParams.setParameter("http.protocol.handle-redirects", false);
HttpGet pageToRequest = new HttpGet("http://graph.facebook.com/" + userId + "/picture?type=large");
pageToRequest.setParams(httpParams);
AndroidHttpClient httpClient = AndroidHttpClient
.newInstance("Android");
HttpMessage httpResponse = httpClient.execute(pageToRequest);
Header header = httpResponse.getFirstHeader("location");
if(header != null){
return(header.getValue());
}
return "";
}
這將返回真正的gif URL(最終URL)。
之后,使用此新URL來解析您的位圖。
改變自:
URL image_value = new URL("http://graph.facebook.com/"+ user.getId()+ "/picture?type=large");
至
URL image_value = new URL(getProfileGif(user.getId());
Bitmap bmp = BitmapFactory.decodeStream(image_value.openConnection().getInputStream());
PS:不要在主線程中執行getProfileGif或任何URL請求。
讓我知道你的結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.