0

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");

        }

    }
});
2
  • 1
    What is the error you're getting? Commented Mar 26, 2016 at 10:17
  • Call your SaveCallback for your ParseFile instead of ParseObject. Commented Mar 26, 2016 at 10:23

2 Answers 2

2

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");

            }

        }
    });}
Sign up to request clarification or add additional context in comments.

5 Comments

Hello Alex, nope, still note working, how can I understand the error?
No, still not, but the upload() function is not starting, the error is in the file saving part. In the first saveCallBack it goes to the error part.
I guess it is a bug in parseServer that is mentioned in the link: github.com/ParsePlatform/parse-server/issues/136
When you add context to the parseFile creation, it starts to work as below: image1 = new ParseFile("profilePhoto.png", byteArray, "image/png"); it is a bug of the parseServer which is fixed in next releases. Thank you very much Alex.
Ohhh! I was offline for sometime,Thats y i didnt seen ur msgs. Anyway, nice to hear that this leaded you in correct way. Happy Coding :)
1

Finally after few hours effort I am able to upload a Parse file on Parse DataBase Steps To Follow: 1 - Write a SignUp Query First. 2 - On Successful response of SignUp Query you will get a Current User Object Id. 3- Then Create One Custom Class in Parse database "profilePictureTable" and add Certain Columns 1- profilePicture. 2- userObjectId. 4- Then Finally Call Callback SaveInBackground

Sample Image that how it will look a like

    Bitmap bitmap = BitmapFactory.decodeFile("Your File Path");
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    final byte[] data = stream.toByteArray();


    final ParseFile file = new ParseFile("profile_pic.png", data);

    file.saveInBackground();

    final ParseObject profilePicture = new ParseObject("profilePictureTable");
    profilePicture.put("profilePicture", file);
    profilePicture.put("userObjectId",ParseUser.getCurrentUser().getObjectId());

    profilePicture.saveInBackground(new SaveCallback()
    {
        @Override
        public void done(ParseException e)
        {
            if (e == null)
            {
                Log.i("Parse", "saved successfully");


            }
            else
            {
                Log.i("Parse", "error while saving");
            }
        }
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.