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

Nav*_*Ali 5 android local-database parse-platform

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();
Run Code Online (Sandbox Code Playgroud)

它给出了以下错误

java.lang.IllegalStateException: Unable to encode an unsaved ParseFile
Run Code Online (Sandbox Code Playgroud)

根据解析文档保存文件file.saveInBackground应该被调用...我认为saveInBackground仅在有互联网连接时才有效...

但我想要实现的是从本地dataStore获取数据(对象或文件),直到与服务器同步...

那么如何在离线模式下以这种方式保存文件,我可以从本地dataStore访问该文件,直到与服务器同步为止...

nav*_*raj 1

您永远不会对文件调用 save (仔细阅读错误:“无法对未保存的 ParseFile 进行编码”)

您必须首先保存文件 - 完成后您可以在引用该文件的对象上调用 save。

我建议您使用回调调用 file.saveInBackground() 。 https://parse.com/docs/android/api/com/parse/ParseFile.html#saveInBackground(com.parse.SaveCallback,com.parse.ProgressCallback

例子:

私有无效 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.
                }
              });
    }
Run Code Online (Sandbox Code Playgroud)

查看 parse 的新本地数据存储。它适用于 ParseObjects,但不适用于 ParseFiles(据我所知)对于离线解决方案,您必须首先固定对象文件并将图像本地存储在 SD 卡上。当对象上的 savecallback 完成时,您可以保存图像(即您知道您已恢复互联网)。当图像保存回调完成时,您可以使用图像的引用来更新对象。

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