我想获取用户的Facebook个人资料图片,但我无法使用我的代码。但是,我成功地获取了用户名,电子邮件和电子邮件。标识。
以下是我为获取用户名,电子邮件,身份证和号码所做的工作个人资料图片:
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);
}
}
此代码未获取用户的个人资料图片。
请让我知道有什么问题!
我第一次使用Facebook API,所以如果有一些愚蠢的错误请合作。
提前致谢。
答案 0 :(得分:0)
添加 new ProfilePicAsync().execute(userID);
userID = response.getJSONObject().getString("id");
<强>在强>
private void getUserDetailsFromFacebook() {...}
并将imageURL = "https://graph.facebook.com/"+ id +"/picture?type=small";
替换为
imageURL = "https://graph.facebook.com/"+ id[0].toString +"/picture?type=small";
答案 1 :(得分:0)
我找到了解决方案。感谢user3069305
我在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;
}