[英]Unable to fetch user's profile picture from facebook. Please see details
I want to fetch user's facebook profile picture but I am unable to do so with my code. 我想获取用户的Facebook个人资料图片,但是我的代码无法做到这一点。 However, I have succeeded in fetching user's name, email & id.
但是,我已经成功获取了用户的姓名,电子邮件和ID。
Here is what I have done to fetch user's name, email, id & profile picture: 这是我获取用户名,电子邮件,ID和个人资料图片的方法:
public class SignUpScreen extends AppCompatActivity {
Button facebookLoginButton;
CircleImageView mProfileImage;
TextView mUsername, mEmailID;
Profile mFbProfile;
ParseUser user;
public String name, email, userID;
public static final List<String> mPermissions = new ArrayList<String>() {{
add("public_profile");
add("email");
}};
private static final String TAG = "SignInActivity";
private static final int RC_SIGN_IN = 9001;
public SignUpScreen() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_sign_up_screen);
TextView textView = (TextView) findViewById(R.id.h);
Typeface typeface = Typeface.createFromAsset(getBaseContext().getAssets(), "fonts/Paci.ttf");
textView.setTypeface(typeface);
mProfileImage = (CircleImageView) findViewById(R.id.user_profile_image);
mUsername = (TextView) findViewById(R.id.userName);
mEmailID = (TextView) findViewById(R.id.aboutUser);
mFbProfile = Profile.getCurrentProfile();
facebookLoginButton = (Button) findViewById(R.id.facebook_login_button);
facebookLoginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ParseFacebookUtils.logInWithReadPermissionsInBackground(SignUpScreen.this, mPermissions, new LogInCallback() {
@Override
public void done(ParseUser user, ParseException err) {
if (user == null) {
Log.d("MyApp", "Uh oh. The user cancelled the Facebook login.");
} else if (user.isNew()) {
Log.d("MyApp", "User signed up and logged in through Facebook!");
getUserDetailsFromFacebook();
final Handler handler3 = new Handler();
handler3.postDelayed(new Runnable() {
@Override
public void run() {
saveNewUser();
}
}, 5000);
} else {
Log.d("MyApp", "User logged in through Facebook!");
}
}
});
}
});
findViewById(R.id.signup_using_email_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final Handler handler3 = new Handler();
handler3.postDelayed(new Runnable() {
@Override
public void run() {
final Dialog dialog = new Dialog(SignUpScreen.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(true);
dialog.setContentView(R.layout.custom_dialog_for_email_signin);
Button dialogButton = (Button) dialog.findViewById(R.id.email_signup_btn);
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Handler handler3 = new Handler();
handler3.postDelayed(new Runnable() {
@Override
public void run() {
dialog.dismiss();
}
}, 200);
}
});
dialog.show();
}
}, 200);
}
});
findViewById(R.id.already_user).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent loginIntent = new Intent(SignUpScreen.this, LoginScreen.class);
startActivity(loginIntent);
}
});
}
public void saveNewUser() {
user = new ParseUser();
user.setUsername(name);
user.setEmail(email);
user.setPassword("hidden");
user.signUpInBackground(new SignUpCallback() {
@Override
public void done(ParseException e) {
if (e == null) {
Toast.makeText(SignUpScreen.this, "SignUp Succesful", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(SignUpScreen.this, "SignUp Unsuccesful", Toast.LENGTH_LONG).show();
Log.d("error when signingup", e.toString());
}
}
});
user.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e == null) {
Toast.makeText(SignUpScreen.this, "saved", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(SignUpScreen.this, "error saving", Toast.LENGTH_LONG).show();
Log.d("error when saving", e.toString());
}
}
});
}
private void getUserDetailsFromFacebook() {
final GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
// Application code
Log.d("response", "response" + object.toString());
//Intent profileIntent = new Intent(SignUpScreen.this, ProfileActivity.class);
//Bundle b = new Bundle();
try {
name = response.getJSONObject().getString("name");
mUsername.setText(name);
email = response.getJSONObject().getString("email");
mEmailID.setText(email);
userID = response.getJSONObject().getString("id");
//b.putString("userName", name);
//b.putString("userEmail", email);
//profileIntent.putExtras(b);
//startActivity(profileIntent);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "name, email, id");
request.setParameters(parameters);
request.executeAsync();
}
class ProfilePicAsync extends AsyncTask<String, String, String> {
Bitmap bmp = null;
@Override
protected String doInBackground(String... id) {
String imageURL;
Toast.makeText(SignUpScreen.this, "Loading picture", Toast.LENGTH_LONG).show();
imageURL = "https://graph.facebook.com/"+ id +"/picture?type=small";
try {
bmp = BitmapFactory.decodeStream((InputStream)new URL(imageURL).getContent());
} catch (Exception e) {
Toast.makeText(SignUpScreen.this, "Loading picture failed", Toast.LENGTH_LONG).show();
e.printStackTrace();
Log.d("Loading picture failed", e.toString());
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
mProfileImage.setImageBitmap(bmp);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
ParseFacebookUtils.onActivityResult(requestCode, resultCode, data);
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
This code is not fetching the user's profile picture. 此代码未获取用户的个人资料图片。
Please let me know what is wrong! 请让我知道怎么了!
I'm using Facebook API for first time, so please cooperate if there are some silly errors. 我是第一次使用Facebook API,因此如果出现一些愚蠢的错误,请配合。
Thanks in advance. 提前致谢。
add new ProfilePicAsync().execute(userID);
添加
new ProfilePicAsync().execute(userID);
below 下面
userID = response.getJSONObject().getString("id");
in 在
private void getUserDetailsFromFacebook() {...}
and replace imageURL = "https://graph.facebook.com/"+ id +"/picture?type=small";
并替换
imageURL = "https://graph.facebook.com/"+ id +"/picture?type=small";
with 同
imageURL = "https://graph.facebook.com/"+ id[0].toString +"/picture?type=small";
I find the solution. 我找到了解决方案。 Thanks to user3069305
感谢user3069305
I did this in doInBackground()
: 我在
doInBackground()
中doInBackground()
:
@Override
protected String doInBackground(String... params) {
String imageURL;
String id = userID;
imageURL = "https://graph.facebook.com/"+ id +"/picture?type=small";
try {
bmp = BitmapFactory.decodeStream((InputStream)new URL(imageURL).getContent());
} catch (Exception e) {
e.printStackTrace();
Log.d("Loading picture failed", e.toString());
}
return null;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.