如何在离线状态下将带有 ParseFile 的 ParseObject 固定到 Parse 本地数据存储? [英] How to pin a ParseObject with a ParseFile to the Parse Local Datastore in OFFLINE?

查看:25
本文介绍了如何在离线状态下将带有 ParseFile 的 ParseObject 固定到 Parse 本地数据存储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码将 ParseObject 与 ParseFile 一起存储.我在 Application 子类中启用了 Parse 本地数据存储.当应用程序连接到互联网时,此代码将 ParseObject 的实例存储在本地数据存储区和解析服务器中.

I am using the following code to store the ParseObject with a ParseFile. I have enabled Parse local datastore in Application subclass. This code storing an instance of the ParseObject in local datastore and in the parse server when the application in 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();                
        }
    });

当应用程序未连接到互联网时,相同的代码抛出以下异常.java.lang.IllegalStateException:无法编码未保存的 ParseFile.该应用程序正在崩溃.对象未存储在本地数据存储中.

Same code when the app is not connected to internet is throwing the following Exception. java.lang.IllegalStateException: Unable to encode an unsaved ParseFile. And the app is crashing. Object is not stored in local datastore.

那么如何在没有互联网的情况下在解析本地数据存储中存储带有 ParseFile 的 ParseObject?

So how can I store a ParseObject with a ParseFile in parse local datastore when there is no internet?

推荐答案

我通过简单地用 ParseObject 存储文件的字节来解决这个问题.当我想要我的文件时,我会将这些字节写回文件.

I have solved this problem by simply storing the bytes of the file with ParseObject. When I want my file, I am writing those bytes back to a file.

我的要求是在解析中存储一个带有名称的录音文件.我按照以下步骤操作:1.获取文件的字节数组2.将字节数组放入ParseObject3. 调用 ParseObject#saveEventually()

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

代码:

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

这篇关于如何在离线状态下将带有 ParseFile 的 ParseObject 固定到 Parse 本地数据存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆