1

I need to save ParseObject with ParseFile, but locally. Method pinInBackground gives error: "Unable to encode an unsaved parse file"

       I can not call file.saveInBackground. Because I need to use offline mode.
       So what should I do?

        //get bitmap
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        mBitmap.compress(Bitmap.CompressFormat.JPEG, 50, baos);

        byte[] data = baos.toByteArray();
        Random random = new Random();

        //create parse File
        final ParseFile file = new ParseFile(random.nextInt(10000) + ".jpeg", data);

        parseObject.put(KEY_IMAGE, file);
        parseObject.pinInBackground(new SaveCallback() {
            @Override
            public void done(ParseException e) {
                //do some action
            }
        });
Bro
  • 53
  • 6
  • Similar to this, https://stackoverflow.com/questions/26671169/how-to-pin-a-parseobject-with-a-parsefile-to-the-parse-local-datastore-in-offlin – Joseph Apr 09 '18 at 22:19

1 Answers1

1

Let me create an answer for all of this.

That error really does tell you what is wrong with the app. You can not pin something that isn't there. Parse pins objects.... you never created an object (yet). Therefore, you can not pin a unsaved object.

You can do two things.

1) If there is no internet connection, display the message to the user and tell them that. Once they restore, they can try again.

2) Use saveEventually. This will save the object once internet hits the device, and then when done, you can pin it. Problem is, if the object doesn't exist and the user wants to see it, they cant.

If it was me, I would go with option 1. If you are asking your user to input files, some sort of connection would be required.

letsCode
  • 2,774
  • 1
  • 13
  • 37
  • "SaveEventually" also give me the same error, So as I understood If I need to put a ParseFile, we need an internet connection for sure. Thanks for explanation. – Bro Apr 11 '18 at 09:37
  • From Parses github, they said saveEventually will work. Then yeah, go with option 1. Best option in my opinion. Maybe star this answer? Thanks.....@Bro – letsCode Apr 11 '18 at 13:13