Java – How do I store and receive a profile picture in Parse.com?

How do I store and receive a profile picture in Parse.com?… here is a solution to the problem.

How do I store and receive a profile picture in Parse.com?

I’m new to parse.com. I searched for hours on how to create a profile picture for a user, but no results. I know how to upload an image to Parse.com, but I don’t know how to receive it.

Here’s how I upload my images :

  // Save new user data into Parse.com Data Storage
                ParseUser user = new ParseUser();
                user.setUsername(usernametxt);
                user.setPassword(passwordtxt);
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                 Compress image to lower quality scale 1 - 100
                photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
                byte[] image = stream.toByteArray();

 Create the ParseFile
                ParseFile file = new ParseFile(usernametxt + ".png", image);
                 Upload the image into Parse Cloud
                file.saveInBackground();

 Create a New Class called "ImageUpload" in Parse
                ParseObject imgupload = new ParseObject("ImageUpload");

 Create a column named "ImageName" and set the string
                imgupload.put("ImageName", usernametxt);

 Create a column named "ImageFile" and insert the image
                imgupload.put("ImageFile", file);

 Create the class and the columns
                imgupload.saveInBackground();

 Show a simple toast message
                Toast.makeText(RegisterActivity.this, "Image Uploaded",
                        Toast.LENGTH_SHORT).show();

user.signUpInBackground(new SignUpCallback() {
                   public void done(ParseException e) {
                        if (e == null) {
                             Show a simple Toast message upon successful registration

Intent intent = new Intent(
                                    RegisterActivity.this,
                                    Welcome.class);
                            startActivity(intent);
                            finish();
                        } else {
                            Toast.makeText(getApplicationContext(),
                                   "Sign up Error", Toast.LENGTH_LONG)
                                    .show();
                        }
                    }
               });
           }

}

This is how I received the image (didn’t work, causing an exception):
ParseQuery query = ParseQuery.getQuery(“ImageUpload”);
query.whereEqualTo(“ImageName”,currentUser.getUsername());
query.getFirstInBackground(new GetCallback() {

      public void done(ParseObject object, ParseException e) {
        if (object != null) {
            Toast.makeText(Welcome.this, currentUser.getUsername(),
                    Toast.LENGTH_SHORT).show();
            ParseFile file = (ParseFile)object.get("ImageFile");
            file.getDataInBackground(new GetDataCallback() {

public void done(byte[] data, ParseException e) {
                if (e == null) {

bitmap=BitmapFactory.decodeByteArray(data, 0, data.length);
                    profilepic.setImageBitmap(bitmap);
                    use this bitmap as you want

} else {
                   something went wrong
                }
              }
            });

} else {
            Toast.makeText(getApplicationContext(), "Exception", Toast.LENGTH_SHORT) .show();

}
      }
    });

It would be helpful if someone would look at the code and try to give me a hint that I was doing something wrong!

Sorry my English is not good.

Solution

Save to Parse – Just call the ParseUser table instead of the ParseObject table

Bitmap bitmapImage = ((BitmapDrawable) profilepic.getDrawable()).getBitmap();  profile pic is the imageview
ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    bitmapImage.compress(Bitmap.CompressFormat.JPEG, 40, stream);

byte[] byteArray = stream.toByteArray();

ParseFile file = new ParseFile("image.png", byteArray);

ParseObject Images = new ParseObject("Images");

Images.put("profilepic", file);
                    Images.put("username", Username);

Images.saveInBackground(new SaveCallback() {
                        @Override
                        public void done(ParseException e) {
                            if (e == null) {
                                Log.i("AppInfo", "Profile pic success");

} else {
                                Log.i("AppInfo", "Profile pic FAIL");
                            }
                        }
                    });

Retrieved from parsing

ParseQuery<ParseUser> imageQuery = new ParseUser.getQuery();
 imageQuery.whereEqualTo("objectId",ParseUser.getCurrentUser().getObjectId());
    imageQuery.findInBackground(new FindCallback<ParseObject>()
    {
        @Override
        public void done(List<ParseUser> users,ParseException e)
        {
            for(ParseUser user : users)
            {
                ParseFile UserProPicFile = object.getParseFile("ImageColumnName");
                byte[] byteArray = new byte[0];

byteArray = UserProPicFile.getData();
                Bitmap ProselectBit = BitmapFactory.decodeByteArray(byteArray,0,byteArray.length); Bitmap with [rofile picture
            }
        }
    });

Hope that helps

Related Problems and Solutions