I am using Parse to save my data from my mobile application in the Sign-Up page. I have a field in my object with type "file", named "photo" which takes an image either from camera or from gallery of phone.
My object named 'User' (already exists - ParseUser-, I just added new fields):
The problem from the below code, is that the image is being saved to the previous user I added with the
user.getCurrentUser().put()
and when I just use the user.put()
, I can't save any data and I see the toast
that "an error has occured".
This is the way I am trying to save all the data:
ParseUser user = new ParseUser();
ParseFile file = null;
user.setPassword(password);
user.setEmail(email);
user.setUsername(username);
user.put("gender", gender);
user.put("age_category", age);
user.put("admin", false);
user.put("premium", false);
user.put("about_me", about);
user.put("reward", 0);
if (flag_photo) {
file = new ParseFile("profile_pic.jpg", image);
user.put("photo", file);
user.saveInBackground();
//user.getParseUser(String.valueOf(user)).saveInBackground();
}
user.signUpInBackground(new SignUpCallback() {
public void done(ParseException e) {
if (e == null) {
// Hooray! Let them use the app now.
Intent intent = new Intent(SignUpActivity.this, LoginActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.push_down_in, R.anim.push_down_out);
finish();
}
else {
int duration = Toast.LENGTH_LONG;
Toast.makeText(SignUpActivity.this, "An error occurred. Please try again!", duration).show();
// Sign up didn't succeed. Look at the ParseException
// to figure out what went wrong
}
}
});
}
Firstly you shouldn't be using user.saveInBackground when creating a new user. The Parse documentation explicitly says 'Note that we used the signUpInBackground method, not the saveInBackground method. New ParseUsers should always be created using the signUpInBackground (or signUp) method. Subsequent updates to a user can be done by calling save.'
Secondly you haven't specified the type of your image variable in the code, however I assume that as it is called image, that it will be a bitmap etc. I believe parse expects a byte[] when trying to create a file. If you need to convert from bitmap to byte[] I suggest you look at this thread: Converting bitmap to byteArray android
Thirdly your problem isnt that it is saving it to the wrong user. Its that you are not saving your file before adding it to your user.
file = new ParseFile("profile_pic.jpg", image);
file.saveInBackground(new SaveCallback() {
public void done(ParseException e) {
// If successful add file to user and signUpInBackground
}
});
Hope this helps!