如何在Android中获取Facebook个人资料图片 [英] How to get Facebook profile image in Android
本文介绍了如何在Android中获取Facebook个人资料图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我看到人们使用
https://graph.facebook.com/me/picture?access_token=ACCESS_TOKEN
提取个人资料图片的API,但我无法绘制如何从中提取个人资料图片。
解决方案
需要调用GraphRequest API来获取用户的所有细节,其中API还提供当前配置文件图片的URL。
Bundle params =新Bundle();
params.putString(fields,id,email,gender,cover,picture.type(large));
new GraphRequest(AccessToken.getCurrentAccessToken(),me,params,HttpMethod.GET,
new GraphRequest.Callback(){
@Override
public void onCompleted(GraphResponse response ){
if(response!= null){
try {
JSONObject data = response.getJSONObject();
if(data.has(picture)){
String profilePicUrl = data.getJSONObject(picture)。getJSONObject(data)。getString(url);
Bitmap profilePic = BitmapFactory.decodeStream(profilePicUrl .openConnection()。getInputStream()) ;
mImageView.setBitmap(profilePic);
}
} catch(异常e){
e.printStackTrace();
}
}
}
})。executeAsync();
I am using Facebook sdk 4.4.0 in android and I want to get current profile picture of the user using graph request. How to do that?
I have seen that people use
https://graph.facebook.com/me/picture?access_token=ACCESS_TOKEN
API to extract profile picture but I cant figure how to extract profile picture from it.
解决方案
First you need to call GraphRequest API for getting all the details of user in which API also gives URL of current Profile Picture.
Bundle params = new Bundle();
params.putString("fields", "id,email,gender,cover,picture.type(large)");
new GraphRequest(AccessToken.getCurrentAccessToken(), "me", params, HttpMethod.GET,
new GraphRequest.Callback() {
@Override
public void onCompleted(GraphResponse response) {
if (response != null) {
try {
JSONObject data = response.getJSONObject();
if (data.has("picture")) {
String profilePicUrl = data.getJSONObject("picture").getJSONObject("data").getString("url");
Bitmap profilePic= BitmapFactory.decodeStream(profilePicUrl .openConnection().getInputStream());
mImageView.setBitmap(profilePic);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}).executeAsync();
这篇关于如何在Android中获取Facebook个人资料图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文