android,在离线模式下保存文件(parse.com)

花门楼前见秋草,岂能贫贱相看老。这篇文章主要讲述android,在离线模式下保存文件(parse.com)相关的知识,希望能为你提供帮助。
parse.com提供了一种使用object.saveEventually(); 在脱机模式下保存对象的方法; call ..这将对象存储在本地dataStore中,除非与服务器进行同步。
问题是,我无法存储下面给定行的文件

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

它给出了以下错误
java.lang.IllegalStateException: Unable to encode an unsaved ParseFile

根据解析文档保存文件file.saveInBackground应该被调用...我认为saveInBackground仅在有互联网连接时才有效...
但我想要实现的是从本地dataStore获取数据(对象或文件),直到与服务器同步...
那么如何在离线模式下以这种方式保存文件,我可以从本地dataStore访问该文件,直到与服务器同步为止...
答案我也遇到了同样的问题,我这样做是为了满足我的要求。我希望它可能会有所帮助。
How to pin a ParseObject with a ParseFile to the Parse Local Datastore in OFFLINE?
另一答案最后保存你的对象,并在saveeventually回调调用parsefile的saveinbackground,并再次将该文件放入你的对象并最终保存。这将使您的文件保持脱机状态
另一答案你永远不会在文件上调用call save(仔细阅读错误:“无法编码UNSAVED ParseFile”)
您必须先保存文件 - 完成后,您可以在引用该文件的对象上调用save。
我建议你使用回调调用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. } }); }

查看parse的新本地数据存储。它适用于ParseObjects但不适用于ParseFiles(据我所知)对于离线解决方案,您必须首先固定目标文件并将图像本地存储在SD卡上。当对象上的savecallback完成时,您可以保存图像(即您知道已恢复Internet)。当图像savecallback完成时,您可以使用参考图像更新对象。
https://parse.com/docs/android/api/com/parse/ParseObject.html#pinAllInBackground(java.lang.String,java.util.List,com.parse.SaveCallback)
另一答案我最近遇到了同样的情况。 @vaibhav的答案对我有用,直到设备离线时未保存的对象和应用程序从任务管理器关闭。如果应用程序在saveEventually()的回调运行之前终止,我注意到回调将不再运行。这导致parseFile没有被保存。
我使用的解决方法是首先将字节数组存储为parseObject的一部分。像这样
object.put("imgBytes", byteArray);

当您在对象上调用saveEventually时,字节将随其一起出现在服务器上。但是当你从解析中检索对象时,它需要更长的时间,因为它也会向你发送字节。所以我决定使用云代码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(); }});

【android,在离线模式下保存文件(parse.com)】使用上面的代码,当您检索对象时,它就像一个普通的parsefile。即使应用程序终止,parsefile仍将保存并链接到该对象。请记住更改为您的班级名称。

    推荐阅读