loginBtn.setUserInfoChangedCallback(new UserInfoChangedCallback() {
@Override
public void onUserInfoFetched(GraphUser user) {
if (user != null) {
userName.setText("Hello, " + user.getName());
Bitmap bitmap1 = getFacebookProfilePicture(user.getId());
imgview.setImageBitmap(bitmap1);
} else {
userName.setText("You are not logged");
}
}
});
public Bitmap getFacebookProfilePicture(String userID) {
try {
URL imageURL = new URL("https://graph.facebook.com/" + userID
+ "/picture?type=large");
bitmap = BitmapFactory.decodeStream(imageURL.openConnection()
.getInputStream());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bitmap;
}
这是我的代码我试图从Facebook获取个人资料照片并在我的应用程序中的图像视图中显示我能够从Facebook获取名称但是当我为获取个人资料照片时放置代码然后我无法获取图像在android中请告诉我哪里做错了给我解决方法如何设置这段代码。
答案 0 :(得分:1)
使用此方法从Url获取位图。
/**
* Download image from server url and return bitmap
*
* @param stringUrl Imaage download url
* @return Bitmap receieved from server
*/
private Bitmap downloadImage(String stringUrl) {
URL url;
Bitmap bm = null;
try {
url = new URL(stringUrl);
URLConnection ucon = url.openConnection();
InputStream is;
if (ucon instanceof HttpURLConnection) {
HttpURLConnection httpConn = (HttpURLConnection) ucon;
int statusCode = httpConn.getResponseCode();
if (statusCode == 200) {
is = httpConn.getInputStream();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
BufferedInputStream bis = new BufferedInputStream(is, 8192);
ByteArrayBuffer baf = new ByteArrayBuffer(1024);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
byte[] rawImage = baf.toByteArray();
bm = BitmapFactory.decodeByteArray(rawImage, 0, rawImage.length);
bis.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return bm;
}
将Facebook个人资料照片网址传递给此方法,并将下载的位图设置为imageview。
URL imageURL = new URL("https://graph.facebook.com/" + userID
+ "/picture?type=large");
imgview.setImageBitmap(downloadImage(imageURL));
我希望它有所帮助!
答案 1 :(得分:0)
使用回调,它们更容易。 按如下方式创建回调;
private FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {
if(jsonObject != null){
try {
String name = jsonObject.getString("name");
String email = jsonObject.getString("email");
String id = jsonObject.getString("id");
saveCurrentProfileInfo(id, name, email);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id, email, name");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
createSnackBar("Facebook login cancelled");
}
@Override
public void onError(FacebookException e) {
createSnackBar("Error logging in to Facebook");
}
};
然后在你的oncreate方法上,初始化一个CallbackManager
CallbackManager callbackManager = CallbackManager.Factory.create();
loginButton.registerCallback(callbackManager, callback);
然后,您可以使用检索到的ID来获取您的个人资料照片。
答案 2 :(得分:0)
假设您拥有正确的访问令牌,更新后的API会以JSON格式显示图片字段:
{
"id": "8169595750129122",
"name": "A user name",
"picture": {
"data": {
"is_silhouette": false,
"url": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xtp1/v/t1.0-1/p50x50/11150668_881451758563505_2749775492727269008_n.jpg?oh=35239d96bf3a6d34bd455c51218412d9&oe=56487861&__gda__=1446869670_cd2511b71fc9b8809d6b52bdbb451ff0"
}
}
}
如果您想在登录后获取图片网址
LoginButton login_button = (LoginButton) view.findViewById(R.id.login_button);
callbackManager = CallbackManager.Factory.create();
login_button.setFragment(this);
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
GraphRequest request = GraphRequest.newMeRequest(
AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
try {
//THIS CONTAINS the URL String displaying it is up to you
String PROFPIC_URL = object.getJSONObject("picture").getJSONObject("data").getString("url");
String FIRSTNAME = object.getString("first_name");
} catch (Exception e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,picture,last_name,first_name");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
textview.setText("Facebook Login Cancelled");
}
@Override
public void onError(FacebookException exception) {
textview.setText("Facebook Login Error");
}
});
进行价值观实验