如何用针一个ParseFile来解析本地数据存储在离线一个的parseObject?离线、如何用、数据存储、parseObject

2023-09-06 04:08:59 作者:半步深

我使用下面的code存储的parseObject与ParseFile。我已经启用解析应用程序子类的本地数据存储。这code在本地数据存储和解析服务器存储的parseObject的实例时到Internet连接的应用程序。

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

当应用程序未连接到互联网抛出以下异常相同code。 java.lang.IllegalStateException:无法连接code未保存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.将字节数组的parseObject 3.调用的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()

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