Android save files offline (parse.com)

parse.com provides a way to save an object offline using object.saveEventually (); call .. this stores the object in the local data store if synchronization is not performed with the server.

the problem is that I cannot store the file with the specified lines

ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 80, stream);
byte[] byteArray = stream.toByteArray();
file = new ParseFile("pic.png",byteArray);  
object.put("file.png", file);   
object.saveEventually();

he gives the following error

java.lang.IllegalStateException: Unable to encode an unsaved ParseFile

according to the parsing documentation, to save the file, file.saveInBackground should be called ... and I think that saveInBackground only works if there is an internet connection ...

but the thing I want to implement is to get the data (object or file) from the local data store until the synchronization is done with the server ...

, , ...

+4
4

saveeventually saveinbackground parsefile .

+2

( : " UNSAVED ParseFile" )

- , , .

file.saveInBackground(), . https://parse.com/docs/android/api/com/parse/ParseFile.html#saveInBackground (com.parse.SaveCallback, com.parse.ProgressCallback)

:

private void saveImageOnParseWithCallbacks() {

parseFile = new ParseFile(gate_image.getName(), byte_image);
parseFile.saveInBackground(new SaveCallback() {
    @Override
public void done(ParseException e) {                          
                if (e == null) {
                    saveParseObject(); // this is where you call object.SaveEven....
                }else {
                   e.printStackTrace(System.err);
                }
              }   
             }, new ProgressCallback() {
               @Override
           public void done(Integer percentDone) {
              // Update your progress spinner here. percentDone will be between 0 and 100.
                }
              });
    }

. ParseObjects, ParseFiles ( ) SD-. savecallback , (.. , ). savecallback , .

https://parse.com/docs/android/api/com/parse/ParseObject.html#pinAllInBackground (java.lang.String, java.util.List, com.parse.SaveCallback)

+1

. @vaibhav , , . saveEventually(), , . , parseFile .

, , , parseObject.

object.put("imgBytes", byteArray);

saveEventually , . , , . cloud- beforeSave-, parseFile, . , , ...

Parse.Cloud.beforeSave(Parse.Object.extend("<Name of Class>"), function(request, response){
        var object = request.object;
        var bytes = object.get("imgBytes");
        if(bytes != null){
            var parsefile=new Parse.File("img.png",bytes);
            parsefile.save().then(function(file){
            object.set("ImageParseFile",file);
            object.set("imgBytes",null);
            response.success();

            },function(error){
            console.error("Can't save imageBytes as ParseFile");
            response.error("Error in cloud code before save");     //set to response.error if parsefile is required, otherwise response.success()
            });
        }
        else{
             response.success();
         }

});

Using the code above, it will look like a regular parsefile when retrieving an object. Even when the application is completed, the parsefile will be saved and bound to the object. Remember to change the name of your class.

0
source

Source: https://habr.com/ru/post/1540835/


All Articles