Reputation: 2035
My graphrequest seems to work fine, I have no issue retrieving the User who logs ins Name nor do I have any problem getting their ID. I am trying to store their profile picture for use within my app (By downloading it as a bitmap) but cant seem to succesfully download it. Could someone tell me what I am doing wrong?
//Run the first time we log into Facebook
//connects everything here
private void firstTimeFBlogin() {
GraphRequest request = GraphRequest.newMeRequest(
AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
Log.i("joe", "Heyyyyo");
try {
String userName = response.getJSONObject().getString("name");
String userID = response.getJSONObject().getString("id");
//String hi2 = response.getJSONObject().getString("first_name");
//String hi3 = response.getJSONObject().getString("gender");
final JSONObject mPicture = object.getJSONObject("picture");
final JSONObject mPictureData = mPicture.getJSONObject("data");
final String mImageUrl = mPictureData.getString("url");
Log.i("joe", "User's Facebook Name: " + userName);
Log.i("joe", ParseUser.getCurrentUser().getUsername());
Log.i("joe", mImageUrl);
Log.i("joe", userID);
ParseUser.getCurrentUser().put("name", userName);
ParseUser.getCurrentUser().put("iUrl", mImageUrl);
ParseUser.getCurrentUser().put("fbID", userID);
ParseUser.getCurrentUser().saveInBackground();
profilePictureRetriever(userID);
} catch (JSONException e) {
Log.i("joe", "Couldn't Succesfully retrieve the stuff...");
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,link,picture");
request.setParameters(parameters);
request.executeAsync();
}
public void profilePictureRetriever(String id) {
Log.i("joe", "Profile Picture Checker");
//Bitmap bitmap = getFacebookProfilePicture(id);
//this is here for test purposes
//tried just maually putting the url in..
Bitmap bm = DownloadImageBitmap("https://graph.facebook.com/849993771766163/picture?type=square");
}
public static Bitmap DownloadImageBitmap(String url) {
Bitmap bm = null;
try {
URL aURL = new URL(url);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
} catch (IOException e) {
Log.e("IMAGE", "Error getting bitmap", e);
}
return bm;
}
}
Is there another/better way to be doing this?
Thanks so much!
Upvotes: 1
Views: 691
Reputation: 35539
Update
Pass required parameter in bundle, as id,name,email,gender, birthday,picture
is passed in below code.
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
// App code
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender, birthday,picture");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
Log.e(TAG, "Cancel");
}
@Override
public void onError(FacebookException exception) {
Log.e(TAG, "Error");
Log.e(TAG, exception.toString());
}
});
Old Answer
get it by calling
graph.facebook.com/<FB UserId>/picture?type=large
Upvotes: 1
Reputation: 61
following code is put after login success.
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(),new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object,GraphResponse response) {
Log.d("info",object.toString()+"");
String id = object.getString("id");
String profilePic ="http://graph.facebook.com/"+id+"/picture";
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,first_name,last_name,age_range,gender,locale,timezone,updated_time,verified,email");
request.setParameters(parameters);
request.executeAsync();
Upvotes: 0
Reputation: 12861
I have done something like this:
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(token, "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 = getFacebookProfilePicture(profilePicUrl);
// set profilePic bitmap to imageview
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}).executeAsync();
You can use this method which returns current Profile picture of User from URL
which we get from above GraphRequest API
.
public static Bitmap getFacebookProfilePicture(String url){
Bitmap bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());
return bitmap;
}
I hope it helps!
Upvotes: 3