【问题标题】:Create new ParseUser from facebook data从 facebook 数据创建新的 ParseUser
【发布时间】:2014-03-19 22:20:42
【问题描述】:

我正在开发应用程序,用户可以在其中注册,自行填写所有必需的信息,或添加电子邮件、Facebook 个人资料图片等信息。

我遵循了这个教程:https://parse.com/tutorials/integrating-facebook-in-android

到目前为止,当用户点击 Facebook 登录按钮 eaven 个人资料图片时,我已经完成了所有输入字段的填写。但是要创建新的 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 用户的个人资料图片。

我提出请求的代码示例:

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 获取信息并填写所有字段的代码:

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[]。

我们将不胜感激。谢谢

【问题讨论】:

    标签: android facebook facebook-graph-api parse-platform


    【解决方案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();
    
    }
    

    将此字节[] 传递给您的 ParseFile 使用

    ParseFile saveImageFile= new ParseFile("profileImage.jpg",signUp_Image_Bytes);
    saveUserDetails.put("YourParseImageFileColumnname",saveImageFile);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-06
      • 2018-11-13
      • 2022-07-31
      • 2018-04-12
      相关资源
      最近更新 更多