Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
528 views
in Technique[技术] by (71.8m points)

android - How to pin a ParseObject with a ParseFile to the Parse Local Datastore in OFFLINE?

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

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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...