我正在开发应用程序,用户可以通过自己注册填写所有必需的信息,或者添加来自Facebook的电子邮件,个人资料图片等信息。
我遵循了本教程:https://parse.com/tutorials/integrating-facebook-in-android
到目前为止,我已经设法在用户点击Facebook登录按钮eaven profile picture时填写了所有输入字段。但是要创建新的parseUser,我需要插入所有这样的值
ParseUser newUser = new ParseUser();
newUser.setUsername(email);
newUser.setPassword(password);
newUser.setEmail(email);
newUser.put("name", name);
newUser.put("phone", phone);
将个人资料图片添加到用户我需要获取Byte []并将其放入newUser中。但我不知道如何从facebookId到Byte []因为在本教程中我从facebookId获得了facebook用户的profiel图片。
我提出请求的代码示例:
private void makeRequest() {
Request request = Request.newMeRequest(ParseFacebookUtils.getSession(), new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
JSONObject userProfile = new JSONObject();
try {
userProfile.put("facebookId", user.getId());
userProfile.put("name", user.getName());
if (user.getLocation().getProperty("name") != null) {
userProfile.put("location", (String) user.getLocation().getProperty("name"));
}
if (user.getProperty("email") != null) {
userProfile.put("email",(String) user.getProperty("email"));
}
if (user.getBirthday() != null) {
userProfile.put("birthday",user.getBirthday());
}
ParseUser currentUser = ParseUser.getCurrentUser();
currentUser.put("profile", userProfile);
currentUser.saveInBackground();
updateprofileinfo();
} catch (JSONException e) {
Toast.makeText(FBLogin.this, "json error ", Toast.LENGTH_LONG).show();
}
} else if (response.getError() != null) {
if ((response.getError().getCategory() == FacebookRequestError.Category.AUTHENTICATION_RETRY)
|| (response.getError().getCategory() == FacebookRequestError.Category.AUTHENTICATION_REOPEN_SESSION)) {
Toast.makeText(FBLogin.this, "invalid fb session", Toast.LENGTH_LONG).show();
startLoginActivity();
} else {
Toast.makeText(FBLogin.this, "error", Toast.LENGTH_LONG).show();
}
}
}
});
request.executeAsync();
}
这是我获取信息的代码,并填写来自facebook的所有字段whit信息:
private void updateprofileinfo() {
ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser.get("profile") != null) {
JSONObject userProfile = currentUser.getJSONObject("profile");
try {
if (userProfile.getString("facebookId") != null) {
String facebookId = userProfile.get("facebookId").toString();
mFbFoto.setProfileId(facebookId);
} else {
mFbFoto.setProfileId(null);
}
if (userProfile.getString("name") != null) {
mFbName.setText(userProfile.getString("name"));
} else {
mFbName.setText("");
}
if (userProfile.getString("email") != null) {
mFbEmail.setText(userProfile.getString("email"));
}
else {
mFbEmail.setText("");
}
}catch (JSONException e) {
Toast.makeText(FBLogin.this, "error update info", Toast.LENGTH_LONG).show();
}
}
}
正如您所看到的,我从请求facebookId获取并将其插入到profilepicture小部件中但是要上传此图片我需要将其转换为byte []然后从byte []创建新的ParseFile然后将其添加到newUser代码中,但我不知道如何从facebookId创建byte []。
任何帮助都会受到重视。感谢
答案 0 :(得分:1)
一旦获得许可,就可以从Facebook图形api获取位图图像
URL img_value = null;
try {
img_value = new URL("http://graph.facebook.com/"+user.getId()+"/picture?type=large");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Bitmap mIcon1 = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
userImage=mIcon1;
mIcon1=null;
Log.i(TAG_facebook, "image retrieved from facebook");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
将位图转换为字节[]
public byte[] compressAndConvertImageToByteFrom(Bitmap imageBitmap){
ByteArrayOutputStream stream = new ByteArrayOutputStream();
imageBitmap.compress(CompressFormat.JPEG, 70, stream);
return stream.toByteArray();
}
使用
将此字节[]传递给ParseFileParseFile saveImageFile= new ParseFile("profileImage.jpg",signUp_Image_Bytes);
saveUserDetails.put("YourParseImageFileColumnname",saveImageFile);