Search code examples
androidimageparse-platformparse-server

Parse Server Saving Images with Android Not Working


I am trying to upload an image to my parse server on AWS and mongoLab. However whenever I try to add the image with the code below, I got an error, when I try to save the object without the image, it succeeds. Am I doing something wrong. I am trying for more than 10 hours and could not make it work.

ParseFile image1;

Bitmap bm = BitmapFactory.decodeResource(getResources(),
        R.drawable.ph);

ByteArrayOutputStream stream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

image1 = new ParseFile("profilePhoto.png", byteArray);
image1.saveInBackground();


JSONArray myFriendList = new JSONArray();
myFriendList.put("xxxxxxxxxxxxxxx");
myFriendList.put("yyyyyyyyyyyyyyy");

ParseObject userSettingObj = new ParseObject("userSetting");
userSettingObj.put("profileName", profileNameField.getText().toString());
userSettingObj.put("userid", ParseUser.getCurrentUser().getObjectId());
userSettingObj.put("name", nameField.getText().toString());
userSettingObj.put("surname", surnameField.getText().toString());
userSettingObj.put("friendList", myFriendList);

userSettingObj.put("photo", image1);

userSettingObj.saveInBackground(new SaveCallback() {
    @Override
    public void done(ParseException e) {

        if (e == null) {

            System.out.println("saved successfully");

        } else {

            System.out.println("error while saving");

        }

    }
});

Solution

  • Try this and let me know.

        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] byteArray = stream.toByteArray();
    image1 = new ParseFile("profilePhoto.png", byteArray);
    image1.saveInBackground(new SaveCallback() {
            @Override
            public void done(ParseException e) {
    
                if (e == null) {
        upload();
                } else {
    
                }
    
            }
        });
    
        public void upload(){
    JSONArray myFriendList = new JSONArray();
        myFriendList.put("xxxxxxxxxxxxxxx");
        myFriendList.put("yyyyyyyyyyyyyyy");
    
        ParseObject userSettingObj = new ParseObject("userSetting");
        userSettingObj.put("profileName",    profileNameField.getText().toString());
        userSettingObj.put("userid", ParseUser.getCurrentUser().getObjectId());
        userSettingObj.put("name", nameField.getText().toString());
        userSettingObj.put("surname", surnameField.getText().toString());
        userSettingObj.put("friendList", myFriendList);
    
        userSettingObj.put("photo", byteArray);
    
        userSettingObj.saveInBackground(new SaveCallback() {
            @Override
            public void done(ParseException e) {
    
                if (e == null) {
    
                    System.out.println("saved successfully");
    
                } else {
    
                    System.out.println("error while saving");
    
                }
    
            }
        });}