无法从 Facebook 获取用户的个人资料图片。请看详情
Posted
技术标签:
【中文标题】无法从 Facebook 获取用户的个人资料图片。请看详情【英文标题】:Unable to fetch user's profile picture from facebook. Please see details 【发布时间】:2016-01-01 11:29:52 【问题描述】:我想获取用户的 Facebook 个人资料图片,但我无法使用我的代码执行此操作。但是,我已经成功获取了用户的姓名、电子邮件和 ID。
这是我为获取用户的姓名、电子邮件、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);
此代码未获取用户的个人资料图片。
请告诉我有什么问题!
我是第一次使用Facebook API,如果有一些愚蠢的错误,请配合。
提前致谢。
【问题讨论】:
问题到底出在哪里?在你的代码的哪一行?登录是否有效? @Paul 一切正常。只是个人资料图片没有出现。 【参考方案1】:添加 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";
【讨论】:
运行这段代码后得到:01-01 17:26:05.257 18640-18678/com.abc.xyz D/Loading picture failed: java.io.FileNotFoundException: https://graph.facebook.com/[Ljava.lang.String;@2cfd018/picture?type=small
@HammadNasir:尝试新的 ProfilePicAsync().execute(userID.toString);并添加 Log.d("fb id", id);在 doInbackground 中查看 id 中的值
问题是我正在获取getUserDetailsFromFacebook()
中的“id”。如何在doInBackground()
中获得相同的“id”?【参考方案2】:
我找到了解决方案。感谢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;
【讨论】:
以上是关于无法从 Facebook 获取用户的个人资料图片。请看详情的主要内容,如果未能解决你的问题,请参考以下文章
facebook 注册插件 - 获取大型个人资料图片或用户名或用户 ID?
如何在统一引擎中从 Facebook API 获取用户名和用户个人资料图片?
Facebook API - 如何通过 Facebook API 获取 Facebook 用户的个人资料图片(无需用户“允许”应用程序)
无法将 facebook 个人资料图片 (Uri) 上传到 Firebase 存储