How to link ParseObject with ParseFile with Parse Local Datastore in OFFLINE?

I am using the following code to store ParseObject with ParseFile. I turned on the local Parse data store in a subclass of Application. This code stores an instance of ParseObject in the local data store and on the analysis server when the application is connected to the Internet.

final ParseFile file = new ParseFile(position + ".mp4", data); file.saveInBackground(new SaveCallback() { @Override public void done(ParseException e) { ParseObject po = new ParseObject("Recordings"); po.put("code", position); po.put("name", myname); po.put("file", file); po.saveEventually(); } }); 

The same code when the application is not connected to the Internet throws the following Exception. java.lang.IllegalStateException: Unable to encode unsaved ParseFile. And the application crashes. The object is not stored in the local data store.

So how can I store ParseObject with ParseFile in the local parsing when there is no Internet?

+5
source share
3 answers

I solved this problem by simply saving the bytes of the file using ParseObject. When I want my file, I write these bytes back to the file.

My requirement was to save the audio file with a name in parsing. I followed these steps: 1. Get the byte array of the file 2. Put the byte array in ParseObject 3. Call ParseObject # saveEventually ()

The code:

  File file = new File(filePath); FileInputStream fis = null; try { fis = new FileInputStream(file); byte data[] = new byte[(int) file.length()]; fis.read(data); ParseObject obj = new ParseObject("Recordings"); obj.put("name", name); obj.put("data", data); obj.saveEventually(); } catch (IOException e) { e.printStackTrace(); } 
+7
source

I really enjoyed the ashokguju decision. In my case, I wanted to save the picture from the Android device offline, so this is what happened to me:

1.- Just by doing this (ashokgujju solution), you won’t add the file to your syntax table, as SAndroidD pointed out, if you want it, you can use the Parse Cloud aftersave function, something like this:

 Parse.Cloud.afterSave("Recordings_or_WhatEverYourTableIs", function(request){ bytes = request.object.get("data"); //"data" is the name given by ashokgujju at "obj.put("data", data);" if(bytes){ var file = new Parse.File("myFile.png", bytes); //png or whatever you want file.save().then(function(success){ request.object.set("record_or_MyColumnWithParseFile", file); request.object.unset("data"); //optional, if you are not going to use this data, it has no meaning keep it there anymore and you will save some space request.object.save(); },function(error){ //error }); } } 

It worked like a charm! but...

2. But only for the thumbnail, I mean, a very small picture, as soon as I wanted to do the same with the big picture, I came across this:

 com.parse.ParseRequest$ParseRequestException: The object is too large -- should be less than 128 kB 

It took me a while to figure this out, because there are no errors where shown, just like SAndroidD. I had to add a callback to save it in order to see it.

  File file = new File(filePath); FileInputStream fis = null; try { fis = new FileInputStream(file); byte data[] = new byte[(int) file.length()]; fis.read(data); ParseObject obj = new ParseObject("Recordings"); obj.put("name", name); obj.put("data", data); obj.saveEventually(new SaveCallback() { @Override public void done(ParseException e) { Log.i(MyClass.logName," is it everything ok boy? "+e); } }); } catch (IOException e) { e.printStackTrace(); } 

So! to summarize: ashokgujju cool workaround plus my afterSave function will keep any parseFile offline while the file itself is not so big.

+1
source

I found a solution to this problem

ckeckout this code

 File file = new File(imagePath); FileInputStream fis = null; try { fis = new FileInputStream(file); byte data[] = new byte[(int) file.length()]; fis.read(data); final ParseFile f=new ParseFile("img", data); f.saveInBackground(new SaveCallback() { @Override public void done(ParseException arg0) { //if (arg0.equals(null)) { ParseObject oImg=new ParseObject("Image"); oImg.put("img", f); oImg.put("name", edtUN.getText().toString()); oImg.saveEventually(new SaveCallback() { @Override public void done(ParseException arg1) { System.out .println("img save..."); } }); //} } }, new ProgressCallback() { @Override public void done(Integer arg0) { //display img upload progress } }); 

and then, when the network is turned on and where it’s open, write in the onResume () method of your first action

 ParseQuery<ParseObject> queryImg = ParseQuery.getQuery("Image"); queryImg.fromPin(ApplicationParse.TODO_GROUP_NAME); queryImg.whereEqualTo("isDraft", true); queryImg.findInBackground(new FindCallback<ParseObject>() { @Override public void done(List<ParseObject> todos, ParseException e) { if (e == null) { for (final ParseObject todo : todos) { // Set is draft flag to false before syncing to Parse // todo.setDraft(false); todo.saveInBackground(new SaveCallback() { @Override public void done(ParseException e) { //if (e == null) { // Let adapter know to update view if (!isFinishing()) { //refresh list data System.out.println("Image DATA is saved ..... "); } } }); } } else { Log.i("MainActivity","syncTodosToParse: Error finding pinned todos: "+ e.getMessage() ); e.printStackTrace(); } } }); 
0
source

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


All Articles